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'),
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.
- Using existing field values in django update query
- Django database delete specific number of entries
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.
- Can't login to django admin after creating a super user with a custom user model
- How to get object from PK inside Django template?