常用的参数应用实例
from flask import Flask, render_template, url_for, session, request, redirect
app = Flask(__name__, template_folder="templates111", static_folder="jingtaimulu", static_url_path='/static') # √
app.secret_key = "wang"
def confirm(func):
def inner(*args, **kwargs):
if session.get('auth'):
return func(*args, **kwargs)
else:
next_url = request.path[1:]
return redirect(url_for("login") + f"?next={next_url}")
return inner
@app.route('/', endpoint="index")
@confirm
def index():
return "index"
@app.route('/login', methods=["GET", "POST"])
def login():
msg = ""
if request.method == "POST":
auth = request.form.get("auth")
if auth:
session["auth"] = auth
next_url = request.args.get("next", "index")
return redirect(url_for(next_url))
else:
msg = "error"
return render_template("login.html", msg=msg)
@app.route('/shopping/', endpoint="shopping")
def shopping():
return "Shopping"
if __name__ == '__main__':
app.run(debug=True)
参数解析
class Flask(_PackageBoundObject)
def __init__(
self,
import_name,
static_url_path=None, # 静态文件的访问路径, 就相当于别名, 类似于django中的 {% load static %}, 资源的url开头就是这里指定的路径
static_folder='static', # 静态文件目录的路径 默认当前项目中的static目录
static_host=None, # 远程静态文件所用的Host地址, 如CDN的主机地址
host_matching=False, # 是否开启host主机位匹配,是要与static_host一起使用,如果配置了static_host, 则必须赋值为True
subdomain_matching=False, # 理论上来说是用来限制SERVER_NAME子域名的,但是目前还没有感觉出来区别在哪里
template_folder='templates', # template模板目录, 默认当前项目中的 templates 目录
instance_path=None, # 指向另一个Flask实例的路径
instance_relative_config=False, # 是否加载另一个实例的配置
root_path=None # 主模块所在的目录的绝对路径,默认项目目录