美文网首页
[Flask] Python Flask 框架(二)

[Flask] Python Flask 框架(二)

作者: 紫藤lvy | 来源:发表于2023-02-14 10:12 被阅读0次
[Jinjia2] 模板渲染,模板访问对象属性,过滤器属性,控制语句,

项目目录结构:
Flask-learning
        static
        templates
            blog_detail.html
            control.html
            filter.html
            index.html
      app.py

app.py 
------------------------------------------------------------------
from flask import Flask , request ,render_template
from datetime import datetime

app = Flask(__name__)

# 自定义过滤器
def data_format(value,format = "%Y-%d-%m "):
    return value.strftime(format)
app.add_template_filter(data_format,"dformat")

class User:
    def __init__(self,username,email):
        self.username = username
        self.email = email

@app.route('/')
def hello_world():
    user = User(username="li", email= "123@qq.com")
    person  = {
        "name" :"zhangsan",
        "password" : "123"
    }
    return  render_template("index.html" , user = user , person = person)

@app.route("/blog/<id>")
def blog_detail(id):
    return render_template("blog_detail.html",blog_id = id)

@app.route("/fiter")
def filter():
    user = User(username="li", email= "123@qq.com")
    mytime = datetime.now()
    books = [{
        "name" : " sanguo",
        "author" : "luoguanzhong"
    },{
        "name": " shuihu",
        "author": "shinaian"
    },]
    return render_template("fiter.html" , user =user,mytime = mytime,books =books )

@app.route("/control")
def control():
    age = 17
    books = [{
        "name": " sanguo",
        "author": "luoguanzhong"
    }, {
        "name": " shuihu",
        "author": "shinaian"
    }, ]
    return render_template("control.html" ,age = age ,books =books )

if __name__ == '__main__':
    app.run(debug=True)

------------------------------------------------------------

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
This is Main page!
{{user.username}}
{{user.email}}
{{person.name}}
</body>
</html>

---------------------------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>过滤器</title>
</head>
<body>
{{user.username}} - {{user.username | length }}
<div> {{ mytime  | dformat}} </div>
</body>
</html>


--------------------------------------------------------------------------

comtrol.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>控制语句</title>
</head>
<body>

{% if age > 18 %}
    <div> 已经成年</div>
{% elif age < 18 %}
    <div> 未成年</div>
{% else %}
    <div> 成年</div>
{% endif %}

{% for book in books %}
    <div>{{book.name}} , {{ book.author }}</div>
{% endfor %}
</body>
</html>

相关文章

  • Python学习

    python-flask框架学习(二) 1、回顾Flask 2、Flask route路由 2.1、静态路由 ap...

  • Python Flask Web框架学习资料汇总

    Python Flask Web框架Flask 官网中文教程(V1.0.2)flask基础之一基于 Python ...

  • Python Web框架 Flask - Flask

    python常用框架 Django + Flask + Tornado + Twisted Flask微框...

  • python flask安装和命令

    Flask Web开发实战学习笔记 Flask简介 Flask是使用Python编写的Web微框架。Web框架可以...

  • 2018-07-04

    Flask的使用 Flask是一个Python Web框架。与Django相比,Flask更为轻量化。(Flask...

  • Flask 基本流程

    Flask 基本流程 一、Flask简介 Flask是一个基于Python实现的web开发'微'框架。 Flask...

  • flask.pocoo.org/doc/0.12——Flask文

    关于Flask Flask是一个用于Python的微型Web开发框架。 有用的链接 Flask网站 Flask@P...

  • Flask-SQLAlchemy

    Flask-SQLALchemy 介绍 Flask-SQLAlchemy 是 Python Web 框架 Flas...

  • Flask解析(一)路由

    flask 简介: Flask是Python一个Web框架 Flask 官网上对它的定位是一个“微” python...

  • flask快速入门

    Flask简介 Flask框架是一个基于python的微型的web框架.微,并不是说Flask功能比较弱,微框架的...

网友评论

      本文标题:[Flask] Python Flask 框架(二)

      本文链接:https://www.haomeiwen.com/subject/gekekdtx.html