python flask 快速搭建 WEB 实战
tags: flask
文章目录
python flask 快速搭建 WEB 实战1. app.py配置首页2. views.py配置首页3. templates配置首页4. 设置变量5. 接口变量6. 接口传参7. 接口返回json8. 接口跳转9. index.html添加javascript10. 设置风格视频:https://mp./s/Az1LmhgYjURjsqJW4cBcLg
原创:/watch?v=kng-mJJby8g
github:/Ghostwritten/flask_simple_demo.git
python -m pip install flaskmkdir flask_web1cd flask_web1mkdir flask_web1/templatemkdir flask_web1/statictouch app.py views.py
1. app.py配置首页
app.py
from flask import Flaskapp = Flask(__name__)@app.route("/")def home():return "this is the home page"if __name__=='__main__':app.run(debug=True, port=8080)
运行
访问:http://127.0.0.1:8080
2. views.py配置首页
app.py
from flask import Flaskfrom views import viewsapp = Flask(__name__)app.register_blueprint(views, url_prefix="/views")if __name__=='__main__':app.run(debug=True, port=8080)
views.py
from flask import Blueprintviews = 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_templateviews = Blueprint(__name__, "views")@views.route("/")def home():return render_template("index.html")
app.py不变
运行:
4. 设置变量
views.py
from flask import Blueprint, render_templateviews = 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_templateviews = 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, requestviews = Blueprint(__name__, "views")@views.route("/")def home():return render_template("index.html",name="Tim")@views.route("/profile")def profile():args = request.argsname = args.get('name')return render_template("index.html",name=name)
7. 接口返回json
views.py
from flask import Blueprint, render_template, request, jsonifyviews = Blueprint(__name__, "views")@views.route("/")def home():return render_template("index.html",name="Tim")@views.route("/profile")def profile():args = request.argsname = args.get('name')return render_template("index.html",name=name)@views.route("/json")def get_json():return jsonify({'name': 'liming','coolness': 10})
8. 接口跳转
views.py
from flask import Blueprint, render_template, request, jsonify,redirect, url_forviews = Blueprint(__name__, "views")@views.route("/")def home():return render_template("index.html",name="Tim")@views.route("/profile")def profile():args = request.argsname = args.get('name')return render_template("index.html",name=name)@views.route("/json")def get_json():return jsonify({'name': 'liming','coolness': 10})@views.route("/data")def get_data():data = request.jsonreturn jsonify(data)@views.route("/go-to-home")def go_to_home():return redirect(url_for("views.get_json"))
回车go-to-home跳转json
修改为views.py:
return redirect(url_for("views.home"))
go-to-home跳转home
9. index.html添加javascript
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><p>hello, {{name }}</p></div><script type="text/javascript"src="{{ url_for('static', filename='index.js')}}"></script></body></html>
创建index.js
touch static/index.js
index.js写入console日志
console.log("I am running");
运行:
10. 设置风格
touch template/profile.html
profile.html写入风格
{% extends "index.html" %}{% block content %}<h1> This is the profile page!</h1>{% endblock %}
views.py
from flask import Blueprint, render_template, request, jsonify,redirect, url_forviews = Blueprint(__name__, "views")@views.route("/")def home():return render_template("index.html",name="Tim")@views.route("/profile")def profile():args = request.argsname = args.get('name')return render_template("profile.html")@views.route("/json")def get_json():return jsonify({'name': 'liming','coolness': 10})@views.route("/data")def get_data():data = request.jsonreturn jsonify(data)@views.route("/go-to-home")def go_to_home():return redirect(url_for("views.home"))
运行:
更多阅读:
linux python web flask 编写 Hello Worldwindows python web flask 编写 Hello Worldpython flask 快速搭建 WEBwindows python web flask 模板开发快速入门windows python flask 与mysql 数据库写入查询windows python flask 返回 json 数据windows python flask 读取文件数据转换表格python flask template 模板应用windows python web route路由详解windows python web flask获取请求参数数据python flask-caching模块缓存详解