python flask 快速搭建 WEB 实战
tags: flask
文章目录
- 1. app.py配置首页
- 2. views.py配置首页
- 3. templates配置首页
- 4. 设置变量
- 5. 接口变量
- 6. 接口传参
- 7. 接口返回json
- 8. 接口跳转
- 9. index.html添加javascript
- 10. 设置风格
视频:https://mp.weixin.qq.com/s/Az1LmhgYjURjsqJW4cBcLg
原创:https://www.youtube.com/watch?v=kng-mJJby8g
github:https://github.com/Ghostwritten/flask_simple_demo.git
python -m pip install flask mkdir flask_web1 cd flask_web1 mkdir flask_web1/template mkdir flask_web1/static touch app.py views.py
1. app.py配置首页
app.py
from flask import Flask app = Flask(__name__) @app.route("/") def home(): return "this is the home page" if __name__=='__main__': app.run(debug=True, port=8080)
运行
2. views.py配置首页
app.py
from flask import Flask from views import views app = Flask(__name__) app.register_blueprint(views, url_prefix="/views") if __name__=='__main__': app.run(debug=True, port=8080)
views.py
from flask import Blueprint views = Blueprint(__name__, "views") @views.route("/") def home(): return "home page"
运行app.py,此效果调用views.py的blueprint
3. templates配置首页
mkdir templates/index.html
自动创建html模板文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> </body> </html>
修改:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Home</title> </head> <body> <div><h1>Home Page</h1></div> </body> </html>
views.py修改:
from flask import Blueprint, render_template views = Blueprint(__name__, "views") @views.route("/") def home(): return render_template("index.html")
app.py不变
运行:
4. 设置变量
views.py
from flask import Blueprint, render_template views = Blueprint(__name__, "views") @views.route("/") def home(): return render_template("index.html",name="Tim",ago=21)
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Home</title> </head> <body> <div><h1>Home Page</h1></div> <p>hello, {{ name }}, you are {{ ago }} years old!</p> </body> </html>
5. 接口变量
views.py
from flask import Blueprint, render_template views = Blueprint(__name__, "views") @views.route("/") def home(): return render_template("index.html",name="Tim") @views.route("/profile/<username>") def profile(username): return render_template("index.html",name=username)
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Home</title> </head> <body> <div><h1>Home Page</h1></div> <p>hello, {{ name }}</p> </body> </html>
运行:
死的用户
6. 接口传参
from flask import Blueprint, render_template, request views = Blueprint(__name__, "views") @views.route("/") def home(): return render_template("index.html",name="Tim") @views.route("/profile") def profile(): args = request.args name = args.get('name') return render_template("index.html",name=name)