Flask框架第二篇.Flask中的配置
from flask import Flask,render_template,send_file import os # 1. template_folder="templatess" 模板文件 存放路径(动态) # 2. static_folder 静态文件存放目录,默认值 static # 3. static_url_path 静态文件访问路径,默认值 / + static_folder #( 用的少) # 4. static_host 静态文件的远程存放路径 # 5. host_matching = False, 如果不是特别需要的话,慎用,否则所有的route 都需要host=""的参数 # 6. subdomain_matching = False, # 理论上来说是用来限制SERVER_NAME子域名的,但是目前还没有感觉出来区别在哪里 app = Flask(__name__,template_folder="templatess",static_folder="img",static_url_path="/static") @app.route("/index") def index(): return render_template("/index.html") if __name__ == '__main__': app.run()
import os from flask import Flask, render_template, send_file app = Flask(__name__,template_folder="templatess",static_folder="img",static_url_path="/static") @app.route("/") def index(): return render_template('index.html') if __name__ == '__main__': app.run()
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h3>hello world!</h3> <img src="/static/1.jpg"> </body> </html>
2. 实例化配置
from flask import Flask,session,jsonify
app = Flask(__name__,template_folder="templatess")
#app.debug = True
app.config["DEBUG"] = True
app.secret_key = "*^%$#^&^$##@#&&*HFRRECHJ"
app.session_cookie_name = "I am not session"
app.permanent_session_lifetime = 20 #单位:秒
app.config["JSONIFY_MIMETYPE"] = "Sakura"
@app.route("/set_session")
def set_session():
session["key"] = 123
return "200 ok"
@app.route("/get_session")
def get_session():
print(session.get("key"))
return jsonify({"k1":"v1"})
if __name__ == '__main__':
app.run()
"""
源码 app.config 进入
default_config = ImmutableDict(
{
"ENV": None, 当前flask运行环境
"DEBUG": None, 编辑模式
"TESTING": False,
"PROPAGATE_EXCEPTIONS": None,
"PRESERVE_CONTEXT_ON_EXCEPTION": None,
"SECRET_KEY": None,
"PERMANENT_SESSION_LIFETIME": timedelta(days=31), 单位:秒
"USE_X_SENDFILE": False,
"SERVER_NAME": None,
"APPLICATION_ROOT": "/",
"SESSION_COOKIE_NAME": "session",
"SESSION_COOKIE_DOMAIN": None,
"SESSION_COOKIE_PATH": None,
"SESSION_COOKIE_HTTPONLY": True,
"SESSION_COOKIE_SECURE": False,
"SESSION_COOKIE_SAMESITE": None,
"SESSION_REFRESH_EACH_REQUEST": True,
"MAX_CONTENT_LENGTH": None,
"SEND_FILE_MAX_AGE_DEFAULT": timedelta(hours=12),
"TRAP_BAD_REQUEST_ERRORS": None,
"TRAP_HTTP_EXCEPTIONS": False,
"EXPLAIN_TEMPLATE_LOADING": False,
"PREFERRED_URL_SCHEME": "http",
"JSON_AS_ASCII": True,
"JSON_SORT_KEYS": True,
"JSONIFY_PRETTYPRINT_REGULAR": False,
"JSONIFY_MIMETYPE": "application/json",
"TEMPLATES_AUTO_RELOAD": None,
"MAX_COOKIE_SIZE": 4093,
}
)
"""
-
setting.py
import hashlib import time class DebugConfig(object): """debug环境""" DEBUG = True SECRET_KEY = "*%$E#KIGF9iafafawfhzsjhzsjzuuufhfwfhwahfff" PERMANENT_SESSION_LIFETIME = 3600 #秒 SESSION_COOKIE_NAME = "I am not session" class TestConfig(object): TESTING = True SECRET_KEY = hashlib.md5(f"{time.time()}*%$E#KIGF9iafafawfhzsjhzsjzuuufhfwfhwahfff{time.time()}".encode("utf-8")).hexdigest() PERMANENT_SESSION_LIFETIME = 360000 #秒 SESSION_COOKIE_NAME = "*&^%$#@$%^&*((()*&^%$$#"
-
test.py
from flask import Flask,session,jsonify from setting import DebugConfig,TestConfig app = Flask(__name__,template_folder="templatess") #快速配置 # app.config.from_object(DebugConfig) app.config.from_object(TestConfig) @app.route("/set_session") def set_session(): session["key"] = 123 return "200 ok" @app.route("/get_session") def get_session(): print(session.get("key")) return jsonify({"k1":"v1"}) if __name__ == '__main__': app.run()