当前位置:网站首页>Basic introduction to flask 6 - Context
Basic introduction to flask 6 - Context
2022-07-16 07:35:00 【Jack_ Chen3】
Flask Context :
Actually in Flask There are two contexts in the project : One is the application context (app), The other is the request context (request).
Request context request And application context current_app Is a global variable . All requests are shared .Flask There is a special mechanism to ensure that the data requested each time is isolated , namely A The data generated by the request will not affect B request . So you can import request object , Will not be affected by some dirty data , And you don't need to use request When it comes to request object . There is no need to understand in detail the specific implementation methods and principles of these two contexts . Just understand the four attributes of these two contexts :
(1)request: Object on request context . This object is generally used to store some requested variables . such as method、args、form etc. .
(2)session: Object on request context . This object is generally used to store some session information .
(3)current_app: Returns the current app.
(4)g: Objects on the application context . Objects used as temporary storage when processing requests .
Common hook function
- before_first_request: Execute before processing the first request . For example, the following code :
@app.before_first_requestdef first_request():
print 'first time request'
2.before_request: Execute before each request . You can usually use this decorator to add some variables to the view function . For example, the following code :
@app.before_requestdef before_request():
if not hasattr(g,'user'):
setattr(g,'user','xxxx')
3.teardown_appcontext: No matter whether there is any abnormality , Registered functions are executed after each request .
@app.teardown_appcontext
def teardown(exc=None):
if exc is None:
db.session.commit()
else:
db.session.rollback()
db.session.remove()
·
4.template_filter: In the use of Jinja2 Customize filters when creating templates . For example, you can add one upper Filter ( Of course Jinja2 This filter already exists ):
@app.template_filter
def upper_filter(s):
return s.upper()
5.context_processor: Context processor . The keys in the returned dictionary can be used in the context of the template . for example :
@app.context_processor
def my_context_processor():
return {'current_user':'Anthony'}
6.errorhandler:errorhandler Receiving status code , You can customize the processing method of the response that returns this status code . for example :
@app.errorhandler(404)
def page_not_found(error):
return 'This page does not exist',404
边栏推荐
- 5年经验之谈:月薪3000到30000,测试工程师的变“行”记...
- 2、 Implementation of software RAID experiment report
- Five principles of aiops landing (3): Architecture route
- Implementation of hash table linear detection class template
- 字节测试总监熬夜10天肝出来的测试岗面试秘籍,给你的大厂梦插上翅膀~
- 【LeetCode】1252. Number of odd value cells
- New features of es6-es11 (this article is enough)
- Multi graph detailed blocking queue - synchronousqueue
- [MySQL] paging query step pit
- 【LeetCode】1217. 玩筹码
猜你喜欢

MySQL - data page

ReentrantLock的公平与非公平核心区别

2021-11-13攻防世界做题记录01MISC

Unity3d record case dots

Leetcode lecture - 873 Length of the longest Fibonacci subsequence (difficulty: medium)

26岁,干了三年自动化,月薪才12k,能跳槽找到一个更高薪资的工作吗?

接口测试与接口测试自动化

【剑指Offer】链表专项总结

week3
![From function test to automatic test, to double the salary, I collated the super complete learning guide [with learning notes]](/img/59/dfc87939871832548acecc8ef1d2bf.jpg)
From function test to automatic test, to double the salary, I collated the super complete learning guide [with learning notes]
随机推荐
Volatile最终解释
JVM对象创建和内存分配机制
SAP OPEN SQL
MySQL error messages errors
From function test to automatic test, to double the salary, I collated the super complete learning guide [with learning notes]
刚接手的新产品怎么快速展开测试
【LeetCode】面试题 01.05. 一次编辑
Scheduledthreadpoolexecutor source code and error explanation
SAP voucher reversal
SAP ABAP BAPI_ MATERIAL_ Availability query available inventory
ThreadLocal source code analysis
Leetcode lecture - 1217 Play chips (difficulty: simple)
Flask基础入门六-上下文
【LeetCode】796. Rotate string
将一个数分解成多个加数相加的形式
Men should be "strong", not "soft", "weak" and "empty"
5年经验之谈:月薪3000到30000,测试工程师的变“行”记...
On the meaning of XML file format in the framework
select / poll / epoll 讲解
Problems encountered in SAP ABAP SMOD & CMOD second generation enhancement