当前位置: 首页 > 工具软件 > flask-website > 使用案例 >

Python Flask Web

谈琦
2023-12-01

3.2向模板传递参数:
index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>这是首页</title>
    <h>这是首页中文字</h>
</head>
<body>

</body>
</html>

user.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>这是用户中心</title>
    <h>欢迎您:{{ name }}</h><br>
    <h>欢迎您:{{ name }}</h><br>
    <h>欢迎您:{{ name }}</h><br>
    <h>欢迎您:{{ name }}</h><br>
    <h>欢迎您:{{ name }}</h><br>
    <h>欢迎您:{{ name }}</h><br>
    <h>欢迎您:{{ name }}</h><br>
    <h>欢迎您:{{ name }}</h><br>
    <h>欢迎您:{{ name }}</h><br>
    <h>欢迎您:{{ name }}</h><br>
    <h>欢迎您:{{ name }}</h><br>
    <h>欢迎您:{{ name }}</h><br>
</head>
<body>

</body>
</html>

app.py

#encoding:utf-8
from flask import Flask
from flask import render_template

app = Flask(__name__)


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


@app.route('/user/<username>')
def user(username):
    return render_template('user.html', name=username)


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

以键值对传递变量
index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>这是首页</title>
    <h1>这是首页中文字</h1>
</head>
<body>
    {{ var1 }}<br>
    {{ var2 }}
</body>
</html>

多个变量值传递给模板
app.py

#encoding:utf-8
from flask import Flask
from flask import render_template

app = Flask(__name__)


@app.route('/')
def index():
    title = '**locals()'
    author = 'k0sec'
    return render_template('index.html', **locals())


@app.route('/user/<username>')
def user(username):
    return render_template('user.html', name=username)


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

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>这是首页</title>
    <h1>这是首页中文字</h1>
</head>
<body>
    {{ title }}<br>
    {{ author }}
</body>
</html>

3.3模板中的控制语句之if语句
if语句使用实例
index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {% if name %}
        <h1>产生的随机数有效</h1>
    {% else %}
        <h1>产生的随机数无效!</h1>
    {% endif %}
</body>
</html>

app.py

#encoding:utf-8
from flask import Flask
from flask import render_template
import random

app = Flask(__name__)


@app.route('/')
def hello_world():
    rand1 = random.randint(0, 1)
    return render_template('index.html', name=rand1)


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

if…elif语句使用实例
index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {% if name==1 %}
        <h1>恭喜,您抽中了一等奖</h1>
    {% elif name==2 %}
        <h1>恭喜,您抽中了二等奖</h1>
    {% else %}
        <h1>恭喜,您抽中了三等奖</h1>
    {% endif %}
    {{ name }}
</body>
</html>

app.py

#encoding:utf-8
from flask import Flask
from flask import render_template
import random

app = Flask(__name__)


@app.route('/')
def hello_world():
    rand1 = random.randint(1, 3)
    return render_template('index.html', name=rand1)


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

3.4模板中的控制语句之for语句
shop.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<table>
    <thead>
    <th>商品名称</th>
    <th>商品价格</th>
    </thead>
    <tbody>
    <meta charset="UTF-8">
    {% for goods in goods %}
        <tr>
            <td>{{ goods.name }}</td>
            <td>{{ goods.price }}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>
</body>
</html>

app.py

#encoding:utf-8
from flask import Flask
from flask import render_template


app = Flask(__name__)


@app.route('/')
def hello_world():
    goods=[{'name': '怪味少女开衫外套春秋韩版学生bf原宿宽松运动风2019新款秋装上衣', 'price': '138'},
           {'name': 'A7seven复古百搭牛仔外套女秋季2019新款宽松显瘦休闲夹克衫上衣', 'price': '100'},
           {'name': '黑色时尚西装外套女春秋中长款2019新款韩版休闲薄款上衣', 'price': '129'}]
    return render_template('shop.html', **locals())


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

 类似资料:

相关阅读

相关文章

相关问答