Django Login and Registration | Django Authentication | Crash Course Tutorial | Rohan Yeole

3 min read 7 months ago
Published on Sep 17, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial is designed to guide you through the process of implementing user authentication and registration in a Django project. By following this step-by-step guide, you will learn how to create secure login and signup functionalities, essential for any web application that requires user management.

Step 1: Setting Up Your Django Project

  1. Install Django: Make sure you have Python and pip installed. Then, run:

    pip install django
    
  2. Create a New Django Project: Use the following command to start a new project:

    django-admin startproject myproject
    

    Replace myproject with your desired project name.

  3. Navigate to Your Project Directory:

    cd myproject
    
  4. Create a Django App: Generate an app for user authentication:

    python manage.py startapp accounts
    
  5. Add the App to Settings: Open settings.py in your project directory and add 'accounts' to the INSTALLED_APPS list.

Step 2: Creating User Models

  1. Define User Registration Model: In accounts/models.py, create a model for user registration. You can use Django's built-in User model or extend it:

    from django.contrib.auth.models import User
    from django.db import models
    
    class Profile(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE)
        # Add additional fields here if needed
    
  2. Run Migrations: Apply your model changes:

    python manage.py makemigrations
    python manage.py migrate
    

Step 3: Creating User Registration Form

  1. Create a Registration Form: In accounts/forms.py, create a form for user registration:

    from django import forms
    from django.contrib.auth.models import User
    
    class UserRegistrationForm(forms.ModelForm):
        class Meta:
            model = User
            fields = ['username', 'password']
    
  2. Add Password Handling: Ensure passwords are hashed. Use Django's built-in user creation methods within your view.

Step 4: Implementing Views for Registration and Login

  1. Create Registration View: In accounts/views.py, create a view to handle user registration:

    from django.shortcuts import render, redirect
    from .forms import UserRegistrationForm
    
    def register(request):
        if request.method == 'POST':
            form = UserRegistrationForm(request.POST)
            if form.is_valid():
                form.save()
                return redirect('login')  # Redirect to login page
        else:
            form = UserRegistrationForm()
        return render(request, 'register.html', {'form': form})
    
  2. Create Login View: Use Django's built-in authentication views for logging in users.

Step 5: Setting Up URLs

  1. Define URLs: In accounts/urls.py, set up the URLs for registration and login:

    from django.urls import path
    from .views import register
    
    urlpatterns = [
        path('register/', register, name='register'),
        # Add login path here
    ]
    
  2. Include App URLs in Project: In your project's urls.py, include the accounts app URLs:

    from django.urls import include
    
    urlpatterns = [
        path('accounts/', include('accounts.urls')),
    ]
    

Step 6: Creating Templates

  1. Create HTML Templates: In the accounts/templates directory, create register.html and login.html files for your forms.
    • Example for register.html:
    <form method="POST">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Register</button>
    </form>
    

Conclusion

By following these steps, you have successfully set up user authentication and registration in your Django project. You can expand this system by adding features like password reset and user profile management. Next, consider exploring Django's built-in authentication views for a more robust solution or implement additional features to enhance your app's functionality. Happy coding!