0
0
DjangoHow-ToBeginner · 4 min read

How to Create a Login View in Django Quickly and Easily

To create a login view in Django, use the built-in LoginView from django.contrib.auth.views and configure it in your urls.py. You can customize the login template and redirect URL to fit your app's needs.
📐

Syntax

The login view in Django is typically created by importing LoginView from django.contrib.auth.views and adding it to your URL patterns. You specify the template for the login form and optionally the redirect URL after login.

  • template_name: The HTML file shown for the login form.
  • redirect_authenticated_user: If True, logged-in users are redirected automatically.
  • next_page: URL to redirect after successful login.
python
from django.contrib.auth.views import LoginView
from django.urls import path

urlpatterns = [
    path('login/', LoginView.as_view(template_name='login.html', redirect_authenticated_user=True), name='login'),
]
💻

Example

This example shows a simple Django login view setup using the built-in LoginView. It uses a custom template login.html and redirects authenticated users to the home page.

python
from django.contrib.auth.views import LoginView
from django.urls import path

urlpatterns = [
    path('login/', LoginView.as_view(template_name='login.html', redirect_authenticated_user=True), name='login'),
]

# login.html (place in your templates folder)
# --------------------------------------------
# <form method="post">
#   {% csrf_token %}
#   <label for="id_username">Username:</label>
#   <input type="text" name="username" id="id_username" required>
#   <label for="id_password">Password:</label>
#   <input type="password" name="password" id="id_password" required>
#   <button type="submit">Log in</button>
# </form>
Output
A login page appears at /login/ where users enter username and password to sign in.
⚠️

Common Pitfalls

Common mistakes when creating a login view in Django include:

  • Not setting template_name, which causes Django to look for the default template and fail if missing.
  • Forgetting to include {% csrf_token %} in the login form, which blocks form submission.
  • Not configuring LOGIN_REDIRECT_URL in settings.py, so users don’t get redirected after login.
  • Using the old function-based views instead of the recommended class-based LoginView.
html
## Wrong: Missing csrf_token in template
# <form method="post">
#   <input type="text" name="username">
#   <input type="password" name="password">
#   <button type="submit">Log in</button>
# </form>

## Right: Include csrf_token
# <form method="post">
#   {% csrf_token %}
#   <input type="text" name="username">
#   <input type="password" name="password">
#   <button type="submit">Log in</button>
# </form>
📊

Quick Reference

Summary tips for creating a login view in Django:

  • Use LoginView from django.contrib.auth.views.
  • Set template_name to your login form HTML.
  • Include {% csrf_token %} in your form for security.
  • Configure LOGIN_REDIRECT_URL in settings.py to control post-login redirect.
  • Use redirect_authenticated_user=True to avoid showing login form to logged-in users.

Key Takeaways

Use Django's built-in LoginView for a simple and secure login page.
Always specify a custom template with a CSRF token for the login form.
Set LOGIN_REDIRECT_URL in settings.py to control where users go after login.
Enable redirect_authenticated_user to prevent logged-in users from seeing the login page.
Avoid legacy function-based login views; prefer class-based LoginView.