Django: Using named urls in @login_required decorator -


most of views in django app use @login_required decorator. also, have 3 different login urls. views have corresponding login urls hardcoded @login_required decorators.

@login_required('myapp/logintype1'): def usertype1_home(request):     # further dode     # ...   @login_required('myapp/logintype2'): def usertype2_home(request):     # further code     # ... 

since number of such views quite large, whenever change login url in urls.py have change login-url in decorators. want use {% urls 'urlpatter1' %} , {% urls 'urlpatter2' %}. can use reverse?

how can use named url patterns instead of hard coding url patterns in @login_required decorator?

somewhere in top of views.py after import ... statements add this

login_type1 = reverse_lazy('urlpatter1') # or login_type1 login_type2 = reverse_lazy('urlpatter2') # or login_type2 

and use these variables later

@login_required(login_url=login_type1) ... 

update: reverse replaced reverse_lazy @alasdair suggested. see docs (2nd point).


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -