文件结构
|-- flask_demo
|-- static
|-- css
|-- 1.css
|-- imgs
|-- 1.png
|-- js
|-- views
|-- index.html
|-- usetemplates.html
|-- web.py
- web.py 文件
from flask import Flask, render_template
app = Flask(__name__, template_folder='views')
# 视图函数绑定多个路由
@app.route('/index')
@app.route('/')
def index():
return '<h1>hello page</h1>'
# 传入参数
# string int float path uuid
# @app.route('/home/<int:user_id>')
# @app.route('/home/<path:user_name>')
# @app.route('/home/<uuid:user_name>')
@app.route('/home/<string:user_name>')
def home(user_name):
print(user_name, type(user_name))
return '<h1>home page</h1>'
# 传入多个参数
@app.route('/unameid/<string:user_name>/<int:user_id>')
def unameid(user_name, user_id):
print(user_name, type(user_name))
print(user_id, type(user_id))
return '<h1>home page</h1>' + user_name + str(user_id)
# 使用注册方式添加路由
def about(user_id):
return 'about page ' + str(user_id)
app.add_url_rule(rule='/about/<int:user_id>', view_func=about)
# 返回模板
@app.route('/temp')
def temp():
return render_template('index.html')
# 返回json
# import json
from flask import jsonify
@app.route('/getjson')
def getjson():
user = {
'static': '1',
'message': 'OK'
}
# return json.dumps(user)
return jsonify(user)
# 重定向
from flask import redirect
@app.route('/baidu')
def baidu():
# 访问该路由重定向到百度
return redirect(location='https://www.baidu.com')
# 地址反解
from flask import url_for
@app.route('/geturl')
def geturl1():
print(url_for('getjson')) # 无参数的视图函数
# @app.route('/home/<string:user_name>')
print(url_for('home', user_name='ken')) # 带参数的视图函数
# return '获取视图函数对应的路由'
return redirect(url_for('home', user_name='ken'))
# 使用模板引擎
@app.route('/usetemp')
def usetemp():
# user = '老王'
# user = '老李'
user = 'bill'
vals = ['ken', 'tom', 'shirly', 'ada', 'bill']
response_info = {'status': '1', 'message': 'Are You OK?'}
return render_template(
'usetemplates.html',
user=user,
vals=vals,
response_info=response_info
)
if __name__ == '__main__':
app.debug = True
app.run()
- 1.css 文件
body{
background-color: gray;
}
-
1.png 图片
-
index.html 文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<link rel="stylesheet" href="/static/css/1.css">
</head>
<body>
<h1>temp page</h1>
<img src="static/imgs/1.png" alt="png" width="10%" height="10%">
<img src="{{ url_for('static', filename='imgs/1.png') }}" alt="png" width="10%" height="10%">
</body>
</html>
- useteimplates.html 文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>使用模板引擎</title>
</head>
<body>
<h1>使用模板引擎</h1>
<!-- 显示python对象 -->
{{ user }} <br>
{{ vals }} <br>
<hr>
{{ response_info }} <br>
{{ response_info['status'] }} <br>
{{ response_info['message'] }} <br>
{{ response_info.status }} <br>
{{ response_info.message }} <br>
{{ response_info.get('message') }} <br>
{{ response_info.get('status') }} <br>
{{ response_info.items() }} <br>
<hr>
<!--if判断-->
{% if user == '老王' %}
<label>是老王</label>
{% elif user == '老李' %}
<label>是老李</label>
{% else %}
<label>不是老王或老李</label>
{% endif %}
<!--for循环-->
<ul>
{% for val in vals %}
<li>{{ val }}</li>
{% endfor %}
</ul>
<ul>
{% for k, v in response_info.items() %}
<li>{{ k }} 键值对 {{ v }}</li>
{% endfor %}
</ul>
<!--过滤器-->
{{ user | upper }} <br>
{{ vals | count }} <br>
{{ vals | length }} <br>
</body>
</html>