15

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,

Christos Hayward
  • 5,777
  • 17
  • 58
  • 113

3 Answers3

24

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'),
keyser
  • 18,829
  • 16
  • 59
  • 101
  • Thank you; I have added (what was unset before) a value of '/accounts/login/'. The behaviour is the same. – Christos Hayward Dec 09 '13 at 20:37
  • @KEYSER, thanks; I'm now getting "TemplateDoesNotExist at /accounts/login/"; it's looking for a "registration/login.html". Do I need to add something to my path? – Christos Hayward Dec 09 '13 at 21:04
  • 4
    Actually, there's a default: You can use the admin login template: (r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'admin/login.html'},name="my_login") See: http://stackoverflow.com/questions/2867213/is-there-a-built-in-login-template-in-django – Haroldo_OK Jun 04 '14 at 20:42
  • @Haroldo_OK yup! (for others, see the comments in [Haroldo's link](http://stackoverflow.com/a/2867342/645270)) – keyser Jun 04 '14 at 21:53
1

What worked for me in Django 2.2.1 - include re_path('^accounts/', admin.site.urls), in my project urls.py:

urls.py

from django.conf import settings
from django.conf.urls import include
from django.conf.urls import re_path
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path('^accounts/', admin.site.urls),
]

And in my views.py:

views.py

from django.contrib.auth.decorators import login_required
from django.views.generic import TemplateView

@method_decorator(login_required, name='dispatch')
class HomePageView(TemplateView):
    """
    Home Page View
    """
    template_name = 'amp/home.html'

Hope that helps.

UPDATE: To avoid warnings from django in regards to admin urls being loaded twice, I used a redirect instead in urls.py:

urls.py

urlpatterns = [
    re_path('^accounts/', admin.site.urls),
    re_path(r'^admin/', RedirectView.as_view(url='/accounts/', permanent=True))
]

More on redirect view here.

radtek
  • 34,210
  • 11
  • 144
  • 111
  • this worked for me (new version of django ). Can u please add some explanation of `re_path(r'^admin/', RedirectView.as_view(url='/accounts/', permanent=True))` – Sami May 20 '21 at 16:29
  • 1
    I added a link to the doc on RedirectView.. you can also get where to redirect to via pattern_name and use a named url pattern from your app. – radtek May 12 '22 at 18:24
1
path('accounts/login/', admin.site.urls),

Add this line in your urls.py project folder. Then it will work fine.

from django.contrib.auth.decorators import login_required
@login_required(login_url='/accounts/login/')

Add above two lines in your views.py file.

Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
Bharath
  • 21
  • 6