Flask 视图,模板,蓝图.
https://www.cnblogs.com/wupeiqi/articles/7552008.html
1. 配置文件
from flask import Flask app =Flask(__name__) # app.secret_key="addd" # app.debug=True app.config.from_object("settings.ProductionConfig") #引入settings文件. print(app.config["DEBUG"]) @app.route("/index") def index(): return "index" if __name__ =="__main__": app.run()
打印结果为
False
settings 文件
SECRET_KEY ="SDADFADF" DEBUG=True class Config(object): DEBUG=True SECRET_KEY="ADFADF" class ProductionConfig(Config): DEBUG=False class TestingConfig(Config): Testing= True
2.路由
endpoint
@app.route("/order",endpoint="n2") def order(): print(url_for("n2")) print(url_for("n2")) return "user" if __name__ =="__main__": app.run()
输出结果:
127.0.0.1 - - [07/Sep/2018 17:41:19] "GET /order HTTP/1.1" 200 - /order /order
不写endpoint 默认是他的函数名称。
@app.route("/index") #默认传递字符串类型
def index(nid,num):
print(nid)
return "index"
@app.route("/order")
def order():
print(url_for("order"))
print(url_for("index"))
print(url_for("index"))
return "user"
@app.route("/xx1111",) def xx(): print(url_for("xx")) print(url_for("index")) print(url_for("order")) return "user"
打印结果:
127.0.0.1 - - [07/Sep/2018 17:56:00] "GET /xx1111 HTTP/1.1" 200 - /xx1111 /index /order
marksafe功能:
全局功能:
flash
蓝图
设置.
from flask import Flask from .views.account import bpaccount from .views.code import bpcode app = Flask(__name__) app.config.from_object("settings.TestingConfig") def create_app(): app=Flask(__name__) app.config.from_object("settings.DevelopmentConfig") app.register_blueprint(bpaccount) app.register_blueprint(bpcode) return app
settings
class Config(object): DEBUG =False SECRET_KEY="ADAFA" class ProductionConfig(Config): DEBUG = False class DevelopmentConfig(Config): DEBUG = False class TestingConfig(Config): TESTING=True
manage
from zzy import create_app app =create_app() if __name__ == '__main__': app.run()
code 视图
from flask import Blueprint bpcode =Blueprint("whq",__name__) @bpcode.route("/code") def hailong(): return "海龙" @bpcode.route("/lizhanqing") def lizhanqing(): return "李占青"
account视图
from flask import Blueprint,render_template bpaccount =Blueprint("xzx",__name__) @bpaccount.route("/login") def hailong(): return render_template("login.html") @bpaccount.route("/logout") def lizhanqing(): return "登出"
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1> 用户登录</h1> </body> </html>
before request 有app级别的,也有蓝图级别的
先说app级别的:
from flask import Flask,session,request,redirect from .views.account import bpaccount from .views.code import bpcode app = Flask(__name__) app.config.from_object("settings.TestingConfig") def auth(): if request.path=="/login": return None user_info =session.get("user_info") if user_info: return None return redirect("/login") def create_app(): print(66666666666) app=Flask(__name__) app.config.from_object("settings.DevelopmentConfig") app.register_blueprint(bpaccount) app.register_blueprint(bpcode) app.before_request(auth) # app级别的在登录 # @app.before_request # def check_login(): # print("checklogin") return app
蓝图级别的
account 视图
from flask import Blueprint,render_template
bpaccount =Blueprint("xzx",__name__)
@bpaccount.before_request
def check_login():
print("before request")
@bpaccount.route("/login")
def hailong():
return render_template("login.html")
code视图
from flask import Blueprint bpcode =Blueprint("whq",__name__) @bpcode.before_request def check_login(): print("before1111111111check") @bpcode.route("/code") def hailong(): return "海龙"
1. 蓝图做目录结构的划分。
2.蓝图中应用before_request应用
3.蓝图做url的划分.
url_prefix url前添加前缀 有点像Django的include
app.register_blueprint(bpaccount,url_prefix="/user") app.register_blueprint(bpcode,url_prefix="/user")
url_for 反向解析,像是django的revers
from flask import Blueprint,render_template,url_for bpaccount =Blueprint("xzx",__name__) @bpaccount.before_request def check_login(): print("before request") @bpaccount.route("/login") def hailong(): print(url_for("xzx.hailong")) print(url_for("xzx.lizhanqing")) print(url_for("whq.hailong")) return render_template("login.html") @bpaccount.route("/logout") def lizhanqing(): return "登出"
结果:
/login /logout /code
staticfiles and templates