Hi everyone.
Forgive my trivial question.
I would like to match the empty url (" ") to the Django login page from “django.contrib.auth.urls”.
In this way, every time the website is opened, the first page will immediately be the login page.
How can I do?
Thanks a lot to everyone.
Two basic options:
One, find the name of the view that you want to use. See Using the Django authentication system | Django documentation | Django.
Then assign that view to the empty url. (Yes, you can have the same view assigned to multiple URLs)
Two, assign the empty url to a RedirectView to redirect the browser to your login url.
Hi @KenWhitesell, thank you for your answer.
I didn’t fully understand you, because in fact i don’t know the name of my Login view, because the login view is incorporate in the “django.contrib.auth.urls”.
So if i don’t know the name of login for i can’t use the login view in my url ‘’ path.
Than you again.
They are documented at Using the Django authentication system | Django documentation | Django
The answer is quite simple. In your urls.py:
from django.urls import include, path
from django.contrib.auth import views as auth_views
urlpatterns = [
path('', auth_views.LoginView.as_view(template_name='registration/login.html'), name='login'),
path('accounts/', include('django.contrib.auth.urls')),
# ... other URL patterns for your project ...
]
Thank you @Polevanov
Now it’s work perfectly!
in your views.py:
from django.contrib.auth.decorators import login_required
@login_required(login_url='login')
def index(request):
return render(request, 'index.html')
In urls.py:
urlpatterns = [
path('', views.index, name='index'),
path('login', auth_views.LoginView.as_view(template_name='registration/login.html'), name='login'),
]