[go: up one dir, main page]

0% found this document useful (0 votes)
28 views5 pages

Django User Registration Authentication Guide

This document provides a step-by-step guide for user registration and authentication in Django, covering project setup, URL configuration, form creation, view implementation, and template setup. It includes instructions for creating registration and login forms, handling user authentication, and optional home view protection. The guide concludes with steps for running and testing the application.

Uploaded by

ekta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

Django User Registration Authentication Guide

This document provides a step-by-step guide for user registration and authentication in Django, covering project setup, URL configuration, form creation, view implementation, and template setup. It includes instructions for creating registration and login forms, handling user authentication, and optional home view protection. The guide concludes with steps for running and testing the application.

Uploaded by

ekta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Step-by-Step Guide: User Registration and Authentication in Django

Step 1: Set Up a Django Project and App


1. Start a new project and app:

django-admin startproject myauthproject

cd myauthproject

python manage.py startapp accounts

2. Add 'accounts' to INSTALLED_APPS in settings.py

Step 2: Configure URLs


In myauthproject/urls.py:

from django.contrib import admin

from django.urls import path, include

urlpatterns = [

path('admin/', admin.site.urls),

path('accounts/', include('accounts.urls')),

Step 3: Create Forms for Registration and Login


In accounts/forms.py:

from django import forms

from django.contrib.auth.models import User

from django.contrib.auth.forms import AuthenticationForm

class RegisterForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)

class Meta:

model = User

fields = ['username', 'email', 'password']

# Optionally use AuthenticationForm for login

Step 4: Create Views for Register, Login, and Logout


In accounts/views.py:

from django.shortcuts import render, redirect

from django.contrib.auth import authenticate, login, logout

from .forms import RegisterForm

def register_view(request):

if request.method == 'POST':

form = RegisterForm(request.POST)

if form.is_valid():

user = form.save(commit=False)

user.set_password(form.cleaned_data['password'])

user.save()

return redirect('login')

else:

form = RegisterForm()

return render(request, 'accounts/register.html', {'form': form})

def login_view(request):
if request.method == 'POST':

username = request.POST['username']

password = request.POST['password']

user = authenticate(request, username=username, password=password)

if user is not None:

login(request, user)

return redirect('home')

return render(request, 'accounts/login.html')

def logout_view(request):

logout(request)

return redirect('login')

Step 5: Create Templates for Registration and Login


Create templates in accounts/templates/accounts/

register.html:

<form method='post'>{% csrf_token %}{{ form.as_p }}<button


type='submit'>Register</button></form>

login.html:

<form method='post'>{% csrf_token %}

Username: <input type='text' name='username'><br>

Password: <input type='password' name='password'><br>

<button type='submit'>Login</button></form>

Step 6: Create URL Patterns in App


In accounts/urls.py:
from django.urls import path

from .views import register_view, login_view, logout_view

urlpatterns = [

path('register/', register_view, name='register'),

path('login/', login_view, name='login'),

path('logout/', logout_view, name='logout'),

Step 7: Optional - Create a Home View


In views.py:

from django.contrib.auth.decorators import login_required

@login_required

def home_view(request):

return render(request, 'accounts/home.html')

Step 8: Run and Test the App


1. Run migrations:

python manage.py makemigrations

python manage.py migrate

2. Run the development server:

python manage.py runserver

3. Visit /accounts/register/ to create a user

4. Visit /accounts/login/ to log in

Summary
✔ User registration via ModelForm
✔ Secure password hashing

✔ User login using Django's authentication system

✔ Logout and optional login protection using @login_required

You might also like