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()
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>
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
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
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 %}
End