flask模板,路由
阿新 • • 發佈:2018-01-30
head itl type gpo [] span .html idt ctype
End
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):flask路由與反向路由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="flask模板 3.flask數據渲染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>
# 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_nameflask條件語句
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_nameflask循環語句
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模板,路由