Contents of articles
1 before_first_Request: execute 2 before the first request after the project starts_ Request: execute 3 after before each request_ Request: after each request, the request will not be executed in case of exception. 4 errorhandler: you can customize the status code of the listening response and handle it: 5 teardown_ Request: a function is bound after each request, and will be executed even if an exception is encountered in ‘non debug’ mode. 6 template_ Global (): global template label 7 template_ Filter: global template filter
1 before_ first_ Request: the first request is executed after the project is started
@app.before_first_request
def before_first_request():
print('Executed on the first request')
2 before_ Request: executed before each request
@app.before_request
def before_request():
print('execute before each request')
# return 'direct return' # If one writes a return return value, then the other before_request will not be executed and the view will not be executed.
be careful:
You can write more than one. If one has written the return value, then the others are before_ The request does not execute, nor does the view.
3 after_ Request: after each request, the request will not be executed in case of exception
def after_request(result):
print('Execute after each request, requests with exceptions will not be executed')
# The result is a wrapped response object that needs to be returned or else an error is reported
return result
4 errorhandler: you can customize the status code of the listening response and process it
@app.errorhandler(404)
def errorhandler(error):
print(error) # is the specific error message
return 'The 404 page ran to Mars'
@app.errorhandler(500)
def errorhandler(error):
print('errorhandlererror message')
print(error)
return 'Server internal error 500'
5 teardown_ Request: a function is bound after each request, which will be executed even if an exception is encountered in non debug
mode.
@app.teardown_request
def terardown_reqquest(error):
print('The view function will be executed after execution regardless of whether the view function has an error')
print('debug cannot be True if you want this function to work')
print('error is the specific error message')
print(error)
6 template_ Global (): global template label
@app.template_global()
def add(a1, a2):
return a1+a2
# {{add(1,2)}}
This can be used as a global label in the template. It can be called directly in the template
{{add(1,2)}}
7 template_ Filter: global template filter
@app.template_filter()
def add_filter(a1, a2, a3):
return a1 + a2 + a3
This can be used as a global filter in the template. It can be called directly in the template. The calling method is the same as template_ The difference between global:
{{1|add_filter(2,3,4)}}
Advantages:
Global template tags and global template filters simplify the need to manually transfer a function to a template call
# app.py
```
def test(a1,a2):
return a1+a2
@app.route('/')
def index():
return render_template('index.html',test=test)
```
# index.html
```
{{test(22,22)}}
```
Read More:
- IIS “Bad Request – Request Too Long. HTTP Error 400. The size of the request headers is too long.”
- Bad Request – Request Too Long. HTTP Error 400. The size of the request headers is too long
- [nodejs] error request aborted after request routing in post mode
- Nginx modifies the front end request size limit (413 request entity too large)
- Error domain = nsurlerrordomain code = – 1001 “request timeout occurred in swift alamofire get request. ” UserInfo={NSUnderlyingErro
- request:fail url not in domain list or Cannot send network request to localhost
- WebView loadrequest request request error “nsurlconnection finished with error – Code – 1022”
- Springboot project: error parsing HTTP request header note: further occurrences of HTTP request parsing
- In the HTML page request Ajax times 400 error, solve Yii submit post form 400 error, and Ajax post request 400 problem (example code)
- errorCode: 500, msg: , result: {“Message”:”There was an error processing the request
- An error occurred while processing your request
- Magento prompts “there has been an error processing your request”
- GeTx reports an error in the get request using getconnect
- There has been an error processing your request magento
- MacPro Your request produced an error. [newNullResponse]
- Solution to Error 400. The request has an invalid header name
- egg.js The frame post request reported an error of invalid CSRF token security verification, which has been solved
- Android network request framework okhttputils reports an error (okhttp3 cannot be found)
- Tomcat error: 500 The server encountered an internal error that prevented it from fulfilling this request
- Solve the problem that the delete request is not available under SpringBoot. There was an unexpected error (type=Method Not Allowed, status=405).