What more do I need to do to have Django's @login_required decorator work?

django, django-authentication, python

Solution

Set the LOGIN_URL in your settings. The default value is `'/accounts/login/'`

The decorator also takes an optional `login_url` argument:

@login_required(login_url='/accounts/login/')

And, from the docs:

Note that if you don’t specify the login_url parameter, you’ll need to ensure that the settings.LOGIN_URL and your login view are properly associated. For example, using the defaults, add the following line to your URLconf:

(r'^accounts/login/$', 'django.contrib.auth.views.login'),

Problem

I am trying to use Django's account system, including the @login_required decorator. My settings.py file includes `django.contrib.auth` and I have done a syncdb. ``` Page not found (404) Request Method: GET Request URL: http://localhost:8000/accounts/login/?next=/ Using the URLconf defined in dashboard.urls, Django tried these URL patterns, in this order: ^$ [name='home'] The current URL, accounts/login/, didn't match any of these. You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page. ``` I see the above after trying to @login_required-decorate my home view. It seems to be choking because it is redirected to accounts/login/, which I have not prepared for in my urls.py. What can I add to urls.py or elsewhere so that the login_required decorator will do its usual behaviour? Thanks,

Original source

Related problems