I want to check that the user is authorized for certain URLs. I\'m using generic views.
The docs here say the login_required can be passed as an optional arguments but I
Those docs are for generic views, which work slightly differently than custom views. Normally login_required is used to decorate a view; if you want to use it within a urlconf then you'll need to write a lambda to wrap the view.
In Django 1.11+, at least, you can do it directly as you want. For example:
# urls.py
from django.contrib.auth.decorators import login_required
urlpatterns = [
# Home path
path('', login_required(TemplateView.as_view(template_name='core/home.html')), name='home'),
# Another paths
# ...
]
In this case, each time you try to enter the homepage, you must be logged in, otherwise you will go to the login screen and then return to your homepage.
you can use decorate_url
http://github.com/vorujack/decorate_url
pip install decorate_url
To use decorators in urls.py you need use real functions instead of their names:
from django.contrib.auth.decorators import login_required
import django.views.generic.date_based as views
urlpatterns = patterns('',
(r'^$', login_required(views.archive_index), link_info_dict,
'coltrane_link_archive_index'),
...