Skip to content
Newer
Older
100644 48 lines (31 sloc) 1.22 KB
573121f @anandology Application processors
anandology authored
1 ---
2 layout: default
ee5d579 add hook to tiltle
nux authored
3 title: Application processors, hooks
573121f @anandology Application processors
anandology authored
4 ---
5
ee5d579 add hook to tiltle
nux authored
6 # Application processors, hooks
7
8 Other languages : [français](/../cookbook/application_processors/fr) | ...
9
10 ## Problem
11
12 How to use application processors, loadhooks and unload hooks?
13
14 ## Solution
15
16 web.py application allows adding processors which can do some processing before and after executing the requests
17
18 def my_processor(handler):
19 print 'before handling'
20 result = handler()
21 print 'after handling'
22 return result
23
24 app.add_processor(my_processor)
25
26 Load hooks and unload hooks can be used to do actions at begining and end of requests.
27
28 def my_loadhook():
29 print "my load hook"
30
31 def my_unloadhook():
32 print "my unload hook"
33
34 app.add_processor(web.loadhook(my_loadhook))
35 app.add_processor(web.unloadhook(my_unloadhook))
36
37 you can write or use global variables in hook function, for example: web.header()
38
39 def my_loadhook():
40 web.header('Content-type', "text/html; charset=utf-8")
41
42 app.add_processor(web.loadhook(my_loadhook))
43
44 ###Tip: and also can use web.ctx or web.input() in hook.
45
46 def my_loadhook():
47 input = web.input()
e53f3ff @smallfish edit
smallfish authored
48 print input
Something went wrong with that request. Please try again.