Does Django automatically recognize registration/login.html as the login template?
Does Django automatically recognize templates/registration/login.html as the login page? I'm working on a Django project and want to use Django's built-in authentication system for user login. I didn't set path to /login in urls.py but it's working on url/accounts/login/?next=/.
My question is:
- Does Django automatically recognize registration/login.html as the default login template?
- If so, how does Django determine this file's location?
- Do I need to set anything in settings.py to make this work?
- Where is written the logic for this built in login
Yes Django automatically recognizes registration/login.html as the default login template when using Django's built-in authentication system django.contrib.auth Here's how it works
How does Django determine this file's location?
Django's LoginView searches for registration/login.html in the directories specified in the TEMPLATES setting under 'DIRS' and in any installed apps that have a templates/registration/login.html file.
For example, if your TEMPLATES setting in settings.py looks like this
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / "templates"], # Django searches here
'APP_DIRS': True, # Django also searches in installed apps
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Django will look for templates/registration/login.html inside your project's BASE_DIR/templates/ and inside the templates/ directories of any installed apps.
Do I need to set anything in settings.py to make this work?
No, by default, Django’s LoginView automatically looks for registration/login.html. However, you can override the default login URL by setting it explicitly in settings.py
LOGIN_URL = "/custom-login/" # Change default login URL
LOGIN_REDIRECT_URL = "/" # Redirect after successful login
If you override the LOGIN_URL you need to define a URL pattern for it in urls.py
from django.contrib.auth import views as auth_views
urlpatterns = [
path("custom-login/", auth_views.LoginView.as_view(template_name="myapp/custom_login.html"), name="login"),
]
Where is the logic for this built-in login?
Django's built-in login logic is implemented in LoginView, which is located in
django/contrib/auth/views.py
The core login logic is defined in LoginView which extends FormView and uses Django’s authentication system
class LoginView(FormView):
template_name = 'registration/login.html'
authentication_form = AuthenticationForm
redirect_field_name = REDIRECT_FIELD_NAME
The actual authentication logic is handled by Django's authenticate() and login() functions, which are located in
django/contrib/auth/__init__.py
The key function handling authentication is
def login(request, user, backend=None):
This function adds the user session and manages authentication.