In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to add decorators to class views in Django development". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Scene:
In Django development, what if we use a class view, such as ListView, DetailView, UpdateView, etc., and we want to add a decorator to this view to achieve some function?
Environment:
Python 3.6
Django 1.11
Incorrect usage and implementation: def is_login (func): def wrapper (request,*args,**kwargs): # if the user is not detected, jump to the login page if not request.session.get ("user"): return redirect (reverse ('login')) return func (request,*args,**kwargs) return wrapper
@ is_loginclass myinfor (generic.ListView): pass error message:
AttributeError: 'function' object has no attribute' as_view'
Correct usage implementation method 1: from django.utils.decorators import method_decorator
Def is_login (func): def wrapper (request,*args,**kwargs): # if the user is not detected, jump to the login page if not request.session.get ("user"): return redirect (reverse ('login')) return func (request,*args,**kwargs) return wrapper
# use method_decorator to wrap the decorator. At the same time, the name parameter is required. Dispatch supports all request types, including get, post, etc. If you specify a request method, change it to: name='get'. Tutorial Origin Server (bigyoung.cn)
@ method_decorator (is_login, name='dispatch') class myinfor (generic.ListView): pass implementation method 2:
Through routing configuration: (not recommended)
Origin server of the tutorial: BigYoung.cn'''
From django.utils.decorators import never_cache
Urlpatterns + = [path ('myinfo/', never_cache (myinfor.as_view ()), name='myinfo'),] Advanced usage:
If you have more than one decorator to decorate, the following are multiple decorators:
Def is_login (func): def wrapper (request,*args,**kwargs): # if the user is not detected, jump to the login page if not request.session.get ("user"): return redirect (reverse ('login')) return func (request,*args,**kwargs) return wrapper
Def is_admin (func): # detects whether it is an admin administrator. Tutorials bigyoung.cn def wrapper (request,*args,**kwargs): pass return wrapper "how to add decorators to class views in Django development" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.