pip3 install Flask-Session
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">from</span> flask <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> Flask
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">from</span> flask <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> session
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">from</span> pro_flask.utils.session <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> MySessionInterface
app </span>= Flask(<span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__name__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">)
app.secret_key </span>= <span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">A0Zr98j/3yX R~XHH!jmN]LWX/,?RT</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">
app.session_interface </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> MySessionInterface()
@app.route(</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">/login.html</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span>, methods=[<span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">GET</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span>, <span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">POST</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">])
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> login():
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">print</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(session)
session[</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">user1</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span>] = <span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">alex</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">
session[</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">user2</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span>] = <span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">alex</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span>
<span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">del</span> session[<span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">user2</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">]
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> <span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">内容</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"</span>
<span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">if</span> <span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__name__</span> == <span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">__main__</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">:
</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">!/usr/bin/env python</span>
<span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);"> -*- coding:utf-8 -*-</span>
<span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> uuid
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> json
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">from</span> flask.sessions <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> SessionInterface
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">from</span> flask.sessions <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> SessionMixin
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">from</span> itsdangerous <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> Signer, BadSignature, want_bytes
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">class</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> MySession(dict, SessionMixin):
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span> <span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__init__</span>(self, initial=None, sid=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">None):
self.sid </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> sid
self.initial </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> initial
super(MySession, self).</span><span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__init__</span>(initial <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">or</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> ())
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span> <span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__setitem__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(self, key, value):
super(MySession, self).</span><span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__setitem__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(key, value)
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span> <span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__getitem__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(self, item):
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> super(MySession, self).<span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__getitem__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(item)
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span> <span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__delitem__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(self, key):
super(MySession, self).</span><span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__delitem__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(key)
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">class</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> MySessionInterface(SessionInterface):
session_class </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> MySession
container </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> {}
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span> <span style="color: rgb(128, 0, 128); background-color: rgb(246, 248, 250);">__init__</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">(self):
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">import</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> redis
self.redis </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> redis.Redis()
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> _generate_sid(self):
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> str(uuid.uuid4())
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> _get_signer(self, app):
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">if</span> <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">not</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> app.secret_key:
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> None
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> Signer(app.secret_key, salt=<span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">flask-session</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">,
key_derivation</span>=<span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">hmac</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">'</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">)
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> open_session(self, app, request):
</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"""</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">
</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"""</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">
sid </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> request.cookies.get(app.session_cookie_name)
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">if</span> <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">not</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> sid:
sid </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self._generate_sid()
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> self.session_class(sid=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">sid)
signer </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self._get_signer(app)
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">try</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">:
sid_as_bytes </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> signer.unsign(sid)
sid </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> sid_as_bytes.decode()
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">except</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> BadSignature:
sid </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self._generate_sid()
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> self.session_class(sid=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">sid)
</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);"> session保存在redis中</span>
<span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);"> val = self.redis.get(sid)</span>
<span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);"> session保存在内存中</span>
val =<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self.container.get(sid)
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">if</span> val <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">is</span> <span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">not</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> None:
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">try</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">:
data </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> json.loads(val)
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> self.session_class(data, sid=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">sid)
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">except</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">:
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> self.session_class(sid=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">sid)
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">return</span> self.session_class(sid=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">sid)
</span><span style="color: rgb(0, 0, 255); background-color: rgb(246, 248, 250);">def</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> save_session(self, app, session, response):
</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"""</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">
</span><span style="color: rgb(128, 0, 0); background-color: rgb(246, 248, 250);">"""</span><span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">
domain </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self.get_cookie_domain(app)
path </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self.get_cookie_path(app)
httponly </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self.get_cookie_httponly(app)
secure </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self.get_cookie_secure(app)
expires </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self.get_expiration_time(app, session)
val </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> json.dumps(dict(session))
</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);"> session保存在redis中</span>
<span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);"> self.redis.setex(name=session.sid, value=val, time=app.permanent_session_lifetime)</span>
<span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);">#</span><span style="color: rgb(0, 128, 0); background-color: rgb(246, 248, 250);"> session保存在内存中</span>
self.container.setdefault(session.sid, val)
session_id </span>=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);"> self._get_signer(app).sign(want_bytes(session.sid))
response.set_cookie(app.session_cookie_name, session_id,
expires</span>=expires, httponly=<span style="color: rgb(0, 0, 0); background-color: rgb(246, 248, 250);">httponly,
domain</span>=domain, path=path, secure=secure)</div></div><div id="mCSB_5_scrollbar_vertical" class="mCSB_scrollTools mCSB_5_scrollbar mCS-minimal-dark mCSB_scrollTools_vertical" style="display: none;"><div class="mCSB_draggerContainer"><div id="mCSB_5_dragger_vertical" class="mCSB_dragger" style="position: absolute; min-height: 50px; top: 0px;"><div class="mCSB_dragger_bar" style="line-height: 50px;"></div></div><div class="mCSB_draggerRail"></div></div></div><div id="mCSB_5_scrollbar_horizontal" class="mCSB_scrollTools mCSB_5_scrollbar mCS-minimal-dark mCSB_scrollTools_horizontal" style="display: block;"><div class="mCSB_draggerContainer"><div id="mCSB_5_dragger_horizontal" class="mCSB_dragger" style="position: absolute; min-width: 50px; display: block; width: 0px; left: 0px;"><div class="mCSB_dragger_bar"></div></div><div class="mCSB_draggerRail"></div></div></div></pre>
pip3 install flask-session
from flask import Flask, session, redirect
from flask.ext.session import Session
app.secret_key = 'asdfasdfasd'
app.config['SESSION_TYPE'] = 'redis'
app.config['SESSION_REDIS'] = Redis(host='192.168.0.94',port='6379')
session['username'] = 'alex'
return redirect('/index')
name = session['username']
总结
以加密的形式放到浏览器的cookie里面。
用户浏览器可以禁用cookie,禁用掉之后就不能用。用户登录就不能成功。
请求进来去cookie中把数据拿到,拿到之后将数据解密并反序列化成字典放到内存,让视图函数使用,视图函数使用完交给其他人,再进行序列化加密放到session中去,
本质:放到session,再给他移除掉。
当请求刚到来:flask读取cookie中session对应的值:eyJrMiI6NDU2LCJ1c2VyIjoib2xkYm95,将该值解密并反序列化成字典,放入内存以便视图函数使用。
视图函数:
-
session是以字典的形式保存在cookie中,字典有啥操作,它就有啥操作。
当请求结束时,flask会读取内存中字典的值,进行序列化+加密,写入到用户cookie中。
可以在配置文件中对相应的配置进行修改。

生命周期默认是31天,可以修改。Django中cookie生命周期默认是2周。
1|8闪现
-
是一个基于Session实现的用于保存数据的集合,其特点是:使用一次就删除。
在session中存储一个数据,读取时通过pop将数据移除。
from flask import Flask,flash,get_flashed_messages
|
flash( 'sdfsdf234234' , 'error' )
|
flash( 'adasdfasdf' , 'info' )
|
print (get_flashed_messages(category_filter = [ 'error' ]))
|
-
1|9中间件
-
请求执行wsgi.app之前和之后定制一些操作,用的是call方法。
Django和Falsk请求源码的入口就是call方法。
call方法什么时候触发?
用户发起请求时,才执行。
任务
在执行call方法之前,做一个操作,call方法执行之后做一个操作。
方式一:改源码

方式二:
class Middleware( object ):
|
def __call__( self , * args, * * kwargs): #4、浏览器发送请求,触发__call__方法
|
ret = self .old( * args, * * kwargs) #5、赋值
|
if __name__ = = '__main__' : #1
|
app.wsgi_app = Middleware(app.wsgi_app) #2、先会给app.wsgi_app赋值
|
app.run() #3、启动werkzeug服务器,等待请求
|
-
Django中的中间件是请求和响应时做一些操作,而Flask中间件是自定义一些操作。在请求执行之前和执行之后定制一些操作。
Flask源码入口:
1|10蓝图(blueprint)
-
目标
给开发者提供目录结构
简单蓝图步骤:
- 在目录下创建views文件夹,放所有的视图,根据不同的业务建相应的py文件
- 在目录下新建templates文件夹,放所有的模板
总结:
其他
1、如果某一个蓝图想在别的地方找模板,怎么办?

如上所示,在实例化蓝图中可以自定义模板位置。那蓝图如果想用模板怎么找?
先找目录下的template下的,没有才去蓝图中找。跟Django一样。(先在项目中找,没找到再去app中去找。)
2、还可以给某一类加前缀。

在每次执行前都要加上前缀,否则报错。
3、可以给某一类添加before_request
from flask import Blueprint,render_template
|
ac = Blueprint( 'ac' ,__name__,template_folder = "xxxxx" )
|
print ( 'app.before_request' )
|
return render_template( 'login.html' )
|
-
这个什么时候用到呢?
登录认证。
只要登录成功才能访问的蓝图中就加before_request.
1|11特殊装饰器
-
before_request
- 谁先定义谁先执行(Django框架中间件的process_request)
after_request
- 谁后定义谁先执行(Django框架中间件的process_response)(内部反转了一下)
flask与django1.9版本之前,不管请求函数有没有return,中间件响应函数都执行。
if __name__ = = '__main__' :
|
-
before_first_request
项目启动起来,第一次请求才执行。
是一个标识,最开始是True,第一次请求之后改为False,就不再执行。
@app .before_first_request
|
if __name__ = = '__main__' :
|
-
template_global
给模板用
-
template_filter
给模板用
-
errorhandler
定制错误页面
-
__EOF__
作 者:高雅
出 处:https://www.cnblogs.com/gaoya666/p/9174665.html
关于博主:编程路上的小学生,热爱技术,喜欢专研。评论和私信会在第一时间回复。或者直接私信我。
版权声明:署名 - 非商业性使用 - 禁止演绎,协议普通文本 | 协议法律文本。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!