Flask学习笔记

12/10/2019 PythonFlask

# 使用config.py设置Flask

#config.py
DEBUG = True
#数据库相关
SECRET_KEY = "SECRET_KEY"
SQLALCHEMY = ""
1
2
3
4
5
# main.py
from flask import Flask
import config
app = Flask(__name__)
app.config.from_object(config)


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

if __name__ == '__main__':
    app.run()
1
2
3
4
5
6
7
8
9
10
11
12
13

# URL传参

@app.route('/article/<articleId>/')
def articleDetail(articleId: str):
    return "%s" % articleId
1
2
3

# URL反转,重定向

from flask import redirect, url_for
url_for("article",articleId="132")
redirect('url')
redirect(url_for("article",articleId="132"))
1
2
3
4

# Jinja2

# 模版渲染和模版传参

index.html位于templates目录下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>{{ usernames }}</p>
<p>{{ username }}</p>
<p>{{ gender }}</p>
<p>{{ age }}</p>
{# 访问模型 #}
<hr>
<p>{{ person.name }}</p>
<p>{{ person.age }}</p>
{# 访问字典 #}
<hr>
<p>{{ dict['google'] }}</p>
<p>{{ dict['baidu'] }}</p>
<p>{{ dict.google }}</p>
<p>{{ dict.baidu }}</p>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# main.py
@app.route('/')
def hello_world():
    class Person(object):
        age = 1
        name = 'Person'
    p = Person()
    context = {
        'username': 'jkm',
        'gender': 'female',
        'age': '23',
        'person': p,
        'dict': {
            "google": 'www.google.com',
            'baidu': 'www.baidu.com'
        }
    }
    return render_template('index.html', **context, usernames="test")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# if 控制语句

# main.py
@app.route('/<isLogin>/')
def hello_world(isLogin):
    if int(isLogin) > 0:
        user = {
            'username': 'test',
            'age': 18
        }
        return render_template('index.html', user=user)
    else:
        return render_template('index.html')
1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {% if user and user.age > 20 %}
        <p>username:{{ user.username }}</p>
        <p>age:{{ user.age }}</p>
    {% else %}
        <a>登陆</a>
        <a>注册</a>
    {% endif %}
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# for循环语句

# 字典遍历

items(),keys(),values()等都可以使用

@app.route('/')
def hello_world():
    user = {
        'username': 'test',
        'age': 21
    }
    return render_template('index.html', user=user)
1
2
3
4
5
6
7
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {% for k, v in user.items() %}
        <p>{{ k }}: {{ v }}</p>
    {% endfor %}
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12

# 数组遍历

@app.route('/')
def hello_world():
    sites = ["https://wwww.baidu.com", "https://wwww.google.com"]
    return render_template('index.html', sites=sites)
1
2
3
4
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {% for value in sites %}
        <a>{{ value }}</a>
    {% endfor %}
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12

# 过滤器

  1. 介绍和语法:

    • 过滤器可以处理变量,把原始的变量经过处理后再展示出来,作用的对象是变量,类似于Linux Shell的管道处理 ls -a | grep name.txt,常用过滤器有default设置默认值,length等
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        {{ avatar | default "url" }}
    </body>
    </html>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

# 模版继承

base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% block container %} {% endblock %}
</body>
</html>
1
2
3
4
5
6
7
8
9
10

index.html

{% extends 'base.html' %}
{% block container %}
<p>index_page</p>
{% endblock %}
1
2
3
4

second.html

{% extends 'base.html' %}
{% block container %}
<p>second_page</p>
{% endblock %}
1
2
3
4

# 加载静态文件css,png等

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="{{ url_for('static',filename='text.css') }}">
</head>
<body>
<img src="{{ url_for('static',filename='images/icon.png') }}" alt="">
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
Last Updated: 7/20/2021, 6:00:39 PM