SoFunction
Updated on 2024-11-18

Flask framework a variety of common decorator examples

In this article, an example of the Flask framework of a variety of common decorators. Shared for your reference, as follows:

The effect is similar to django's process_request decorator.

@app.before_request
def before(*args,**kwargs):
  print('Prior to request')
  '''
  If pass access is allowed, you can return None
  The function decorated by this decorator will directly end the access if it returns anything else.
  The effect is somewhat similar to django's process_reqeust middleware method.
  For example, through this decorator to write the login authentication, determine whether it has a session, no access is not allowed, there is continued access
  Then by judging the access function, if it is login (whitelist) then pass.
   is the full url
  is the domain name after the url regular
  '''
  if  == '/login':
    return None
  user = ('user_info')
  if user:
    return None
  return redirect('/login')

Similar to process_response

@app.after_request
def after(response):
  #The effect is the same as process_response, there must be a return value, if there is not, it will report an error.
  print('I'm leaving')
  return response

Order of execution of flask middleware decorators.

If more than oneapp.before_requestcap (a poem)app.after_request

Then the order of execution is similar to django.

app.before_requestis executed according to the top-down (top and bottom of the document), theapp.after_requestIt is implemented from the bottom up.

If there is a change in theapp.before_requestcenterreturnis blocked, then the view function will not be executed.

Directly from the lastapp.after_reqeustStart executing all of them backwards.after_request

Customize the error page:

@app.error_handlers(404)
def error_404(arg):
  '''Customized error page, customized by status code'''
  return "404 error."

Customization methods in templates:

Decorators for front-end functions that call back-end functions directly:

@app.template_global()
def xx(a1,a2):
  return a1+a2
'''
What this decorator does is that you can call this function on the back end directly from the front end via {{ xx(1,2)}}.
'''

Equivalent to a filter decorator

@app.template_filter()
def db(a1,a2,a3):
  return a1+a2+a3
'''
The effect is similar to django's Filter, you need to pay attention to the writing style when rendering on the front-end
{{ 1|db(2,3)}} 1 is the first parameter, followed by 2,3 parameters.
'''

A decorator that comes to request an operation for the first time:

@app.before_first_request
def first(*args,**kwargs):
  pass
'''
Function decorators that are executed only on the first request
'''

Flask's middleware generally doesn't feel very useful, not as convenient as decorators

By recopyingapp.wsgi_app

Rewrite this class's__call__method, as above, just add two printing, the effect of the sameprocess_request,process_response

More about Python related content can be viewed on this site's topic: thePython introductory and advanced classic tutorials》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand theSummary of Python file and directory manipulation techniques

I hope that what I have said in this article will help you in Python programming.