Flask Request an extension before_request after_request errorhandler

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: