Django Login and Registration | Django Authentication | Crash Course Tutorial | Rohan Yeole
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
-
Install Django: Make sure you have Python and pip installed. Then, run:
pip install django -
Create a New Django Project: Use the following command to start a new project:
django-admin startproject myprojectReplace
myprojectwith your desired project name. -
Navigate to Your Project Directory:
cd myproject -
Create a Django App: Generate an app for user authentication:
python manage.py startapp accounts -
Add the App to Settings: Open
settings.pyin your project directory and add'accounts'to theINSTALLED_APPSlist.
Step 2: Creating User Models
-
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 -
Run Migrations: Apply your model changes:
python manage.py makemigrations python manage.py migrate
Step 3: Creating User Registration Form
-
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'] -
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
-
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}) -
Create Login View: Use Django's built-in authentication views for logging in users.
Step 5: Setting Up URLs
-
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 ] -
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
- Create HTML Templates: In the
accounts/templatesdirectory, createregister.htmlandlogin.htmlfiles for your forms.- Example for
register.html:
<form method="POST"> {% csrf_token %} {{ form.as_p }} <button type="submit">Register</button> </form> - Example for
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!