Skip to content

Code Artisan Lab

Learn PHP, Python & JavaScript

  • Home
  • Services
    • Web Designing
    • Web Development
    • Mobile App Development
    • API Development
  • Softwares
  • Mobile App
  • Training
  • My account
  • Contact Us
  • Vendors
  • Enquiry Now

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

  • Create Invoices Easily with InvoiceUp Create Invoices Easily with InvoiceUp
  • TradeGo - Add Listing Everywhere TradeGo - Add Listing Everywhere
  • Expense Tracker - Easily Track Expense Anytime Expense Tracker - Easily Track Expense Anytime
  • 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
  • Blog (12)
  • Bootstrap (2)
  • Flutter (2)
  • JavaScript (6)
  • JWT (2)
  • Mobile App (4)
  • Mobile App Development (9)
  • Next.js (7)
  • Nuxt.js (1)
  • Offline Training (3)
  • Python Django (15)
  • React .js (5)
  • React Native (5)
  • Services (13)
  • Tailwind (2)
  • Web Development (13)

Recent Posts

  • Learn Tailwind CSS in Ten Days with Easy Step-by-Step Guide
  • Learn Bootstrap in Ten Days with Easy Step-by-Step Guide Plan
  • Bootstrap and Tailwind CSS Comparison for Modern, Responsive Web Development
  • Django vs Python: Understanding Which Is Easier for Developers Today
  • Why JWT Is Better Than Other Authentication Methods for Security?
Proudly powered by WordPress
%d