Skip to content

Code Artisan Lab

Learn PHP, Python & JavaScript

  • Home
  • Softwares
  • Courses
  • Training
  • My account
  • Contact Us
  • Vendors

Django Tutorial Part 3: User Login, Logout & Registration (2025 Beginner Guide)

In Part 2, you learned how to create forms and accept user input. Now it’s time to handle user authentication. In this post, you’ll add login, logout, and registration functionality using Django’s built-in auth system.

What You’ll Learn

  • Enable Django’s auth system
  • Create user registration (sign up) form
  • Add login and logout functionality
  • Secure pages to logged-in users only

Step 1: Configure URLs

Django provides built-in views for login and logout. First, update myproject/urls.py:

1
2
3
4
5
6
7
8
from django.contrib import admin
from django.urls import path, include
 
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),
    path('accounts/', include('django.contrib.auth.urls')),  # Login/Logout
]

Step 2: Create Registration View

Create a view in myapp/views.py:

1
2
3
4
5
6
7
8
9
10
11
12
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
 
def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login')  # Redirect to login after signup
    else:
        form = UserCreationForm()
    return render(request, 'registration/register.html', {'form': form})

Step 3: Create Registration Template

Create templates/registration/register.html:

1
2
3
4
5
6
7
<h2>Sign Up</h2>
 
<form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Register</button>
</form>

Step 4: Add Registration URL

In myapp/urls.py:

1
2
3
4
5
6
7
8
from django.urls import path
from . import views
 
urlpatterns = [
    path('', views.home, name='home'),
    path('create/', views.create_post, name='create_post'),
    path('register/', views.register, name='register'),
]

Step 5: Create Login & Logout Templates

Create templates/registration/login.html:

1
2
3
4
5
6
7
8
9
<h2>Login</h2>
 
<form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Login</button>
</form>
 
<p>Don't have an account? <a href="{% url 'register' %}">Sign up</a></p>

Django automatically logs out users at /accounts/logout/. You can create a basic logout confirmation template at templates/registration/logged_out.html:

1
2
<h2>You have been logged out.</h2>
<a href="{% url 'login' %}">Log in again</a>

Step 6: Secure Views with Login Required

To restrict access to views, use the login_required decorator:

1
2
3
4
5
from django.contrib.auth.decorators import login_required
 
@login_required
def create_post(request):
    ...

Step 7: Redirect After Login

In myproject/settings.py add:

1
2
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/accounts/login/'

Conclusion

You’ve successfully added user authentication to your Django app, including login, logout, and user registration. With authentication in place, you can now build dashboards, user-specific features, and protected content.

Coming Next in Part 4: Learn how to add user profiles and permissions!

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Like this:

Like Loading...
This entry was posted in Blog, Python Django and tagged django auth example, django auth system, django authentication tutorial, django authentication views, django beginner auth, django create user, django csrf login, django custom signup, django login form, django login logout, django login page, django login view, django logout view, django registration, django signup form, django tutorial part 3, django user authentication, django user model, django user register, django web authentication on June 14, 2025 by surajkumar.

Post navigation

← Django Tutorial Part 2: Handling Forms and User Input (2025 Beginner Guide) Django Tutorial Part 4: User Profiles and Permissions (2025 Beginner Guide) →

Products

  • Smart GST Accounts Software Smart GST Accounts Software
  • Complete GST Billing Software Complete GST Billing Software
  • Fast & Easy Restaurant Billing Software Fast & Easy Restaurant Billing Software
  • Easy Appointment Booking Easy Appointment Booking
  • All-in-One Hotel Booking Management Software All-in-One Hotel Booking Management Software
  • Manage Projects Faster with Laravel & MySQL System Manage Projects Faster with Laravel & MySQL System
  • Modern Learning Management System Modern Learning Management System
  • Smart Billing Software – Fast, Secure & Scalable Solution for Your Business Smart Billing Software – Fast, Secure & Scalable Solution for Your Business
  • Next-Gen Accounting Software for Modern Businesses Next-Gen Accounting Software for Modern Businesses
  • Modern Accounting Software Modern Accounting Software
  • Blog (12)
  • Flutter (2)
  • JavaScript (3)
  • Mobile App Development (4)
  • Next.js (1)
  • Python Django (9)
  • React Native (1)
  • Web Development (1)

Recent Posts

  • Flutter App with Django Backend Using DRF and PostgreSQL | Complete Guide 2025
  • Flutter App Creation from Scratch | Beginner’s Guide 2025
  • DRF vs FastAPI vs Django Ninja vs Flask: Best Python Web Frameworks Compared in 2025
  • Django vs Laravel: Which Backend Framework is Better in 2025?
  • React Native vs Flutter: Which Framework is Better for Mobile App Development in 2025?
Proudly powered by WordPress
%d