Implementing User Authentication in Django
π Implementing User Authentication in Django
A Step-by-Step Guide (Login, Logout, and Registration)
✅ What is User Authentication?
Authentication means verifying the identity of a user. In a Django web app, it typically includes:
User Registration (Sign Up)
User Login
User Logout
Password Management
Django provides built-in support for all of these using the django.contrib.auth framework.
π ️ Step 1: Create a Django Project and App
bash
Copy
Edit
django-admin startproject myproject
cd myproject
python manage.py startapp accounts
Add 'accounts' and 'django.contrib.auth' to your INSTALLED_APPS in settings.py:
python
Copy
Edit
INSTALLED_APPS = [
...
'accounts',
'django.contrib.auth',
]
π§© Step 2: URLs and Views Setup
Create accounts/urls.py:
python
Copy
Edit
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
path('login/', auth_views.LoginView.as_view(template_name='accounts/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('register/', views.register, name='register'),
]
Include in the main myproject/urls.py:
python
Copy
Edit
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 User Registration View
accounts/views.py
python
Copy
Edit
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')
else:
form = UserCreationForm()
return render(request, 'accounts/register.html', {'form': form})
π️ Step 4: Templates (HTML Pages)
Create a folder templates/accounts/ and add these files:
register.html
html
Copy
Edit
<h2>Register</h2>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Register</button>
</form>
login.html
html
Copy
Edit
<h2>Login</h2>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
Optional: Add a logout success redirect in settings.py
python
Copy
Edit
LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/accounts/login/'
π Step 5: Protecting Views with Login Required
python
Copy
Edit
from django.contrib.auth.decorators import login_required
@login_required
def dashboard(request):
return render(request, 'accounts/dashboard.html')
π§ͺ Step 6: Run and Test
bash
Copy
Edit
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
Go to /accounts/register/ to create a user
Go to /accounts/login/ to log in
Go to /accounts/logout/ to log out
✅ Bonus: Use django-allauth for Social Login
If you want GitHub, Google, or Facebook login:
bash
Copy
Edit
pip install django-allauth
(Detailed setup available on request)
π§ Summary
Feature Tool / Function
Registration UserCreationForm, custom view
Login LoginView from django.contrib.auth.views
Logout LogoutView
Templates Custom HTML under templates/accounts/
Auth Middleware Enabled by default (AuthenticationMiddleware)
URL Handling include() in main urls.py
Learn Full Stack Python Course in Hyderabad
Read More
Building a Data-Driven Web Application with Python
How to Use SQLAlchemy with Flask for Database Management
Introduction to MongoDB for Full Stack Python
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment