flask模板,路由

Posted neeo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask模板,路由相关的知识,希望对你有一定的参考价值。

1.flask的路由与反向路由

技术分享图片
from flask import Flask, request, url_for
app = Flask(__name__)

@app.route(/)
def hello_world():
    return Hello World!


@app.route(/user, methods=[POST])  # 指定请求方式,默认为GET
def hell_user():
    return hello user


@app.route(/user/<id>)  # http://127.0.0.1:5000/user/123
def user(id):
    return user_id: + id


@app.route(/user_id)  # http://127.0.0.1:5000/user_id?id=2
def user_id():
    id = request.args.get(id)
    return user_id: + id


# 反向路由
@app.route(/user_url)
def user_url():
    return user_url: + url_for(user_id)

if __name__ == __main__:
    app.run()
flask路由与反向路由

2.flask模板

技术分享图片
# views试图
from flask import Flask, request, url_for, render_template


app = Flask(__name__)

@app.route(/index)
def index():
    content = hello world

    return render_template(index.html, content=content)

if __name__ == __main__:
    app.run()

# html页面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>flask模板示例</title>
</head>
<body>

<h3>{{ content }}</h3>

</body>
</html>
flask模板
3.flask数据渲染
技术分享图片
# views试图
from flask import Flask, request, url_for, render_template

@app.route(/index)
def index():
    content = hello world

    return render_template(index.html, content=content)

if __name__ == __main__:
    app.run()

# index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>flask模板示例</title>
</head>
<body>

<h3>{{ content }}</h3>

</body>
</html>
数据渲染

4.flask条件语句

技术分享图片
# views试图
from flask import Flask, request, url_for, render_template
from models import User

app = Flask(__name__)


@app.route(/query/<user_id>)
def query(user_id):
    user = None
    if int(user_id) == 1:
        user = User(1, wang)
    return render_template(if_else.html, user=user)

if __name__ == __main__:
    app.run()

# if_else.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>flask模板示例</title>
</head>
<body>

{% if user %}
    <h2>hello: {{ user.user_name }}</h2>
{% else %}
    <h3>hello: anonymous</h3>
{% endif %}

</body>
</html>

# models.py
class User(object):
    def __init__(self, user_id, user_name):
        self.user_id = user_id
        self.user_name = user_name
flask条件语句

5.flask循环语句

技术分享图片
# views试图
from flask import Flask, request, url_for, render_template
from models import User

app = Flask(__name__)

@app.route(/user_list)
def user_list():
    users = []
    for i in range(1, 21):
        user = User(i, wang + str(i))
        users.append(user)
    return render_template(user_list.html, users=users)

if __name__ == __main__:
    app.run()

# user_list.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>flask模板示例</title>
</head>
<body>

{% for user in users %}
<h4>{{ user.user_id }}--{{ user.user_name }}</h4>
{% endfor %}


</body>
</html>



# models.py

class User(object):
    def __init__(self, user_id, user_name):
        self.user_id = user_id
        self.user_name = user_name
flask循环语句

6.flask模板继承

技术分享图片
# views试图
from flask import Flask, request, url_for, render_template
from models import User

app = Flask(__name__)

@app.route(/inherit_one)
def inherit_one():
    return render_template(inherit_one.html)


@app.route(/inherit_two)
def inherit_two():
    return render_template(inherit_two.html)


if __name__ == __main__:
    app.run()


# base.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>flask模板的继承</title>
</head>
<body>

<h1>HEAD</h1>
{% block content %}

{% endblock %}
<h1>FOOT</h1>


</body>
</html>

# inherit_one.html
{% extends base.html %}

{% block content %}
<h3>this is Num.1 page</h3>
{% endblock %}

# inherit_two.html
{% extends base.html %}

{% block content %}
<h3>this is Num.2 page</h3>
{% endblock %}
flask模板机继承

 

 
 
 
 
 
 

End

 

 

以上是关于flask模板,路由的主要内容,如果未能解决你的问题,请参考以下文章

Flask 1.1.x - 使用模板问题的基本路由

flask 配置文件 路由 视图 模板 中间件

Flask从入门到精通之链接的使用

Flask模板宏的概念和基本使用

flask如何设置模板语言Jinjia?如何查看路由视图函数映射?

12_关于flask中的宏