Login Functionality in Django Using contrib.auth Copy Copy
December 10, 2024 | 0 Comments
info@trickywebsolutions.com |
+1-225-276-2741
Django’s django.contrib.auth
library makes it simple to create user registration (signup) functionality. This blog will guide you through the process step by step.
Run the following commands to create your project and app:
django-admin startproject myproject
cd myproject
python manage.py startapp accounts
In settings.py
, add the accounts
app to INSTALLED_APPS
:
INSTALLED_APPS = [
...,
'accounts',
'django.contrib.auth',
'django.contrib.contenttypes',
...,
]
Run the following command to ensure all required database tables are created:
python manage.py migrate
To enable user registration, we need a signup form and a view.
views.py
In your accounts
app, open or create the views.py
file and add the following code:
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login
def signup(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user) # Log the user in after successful registration
return redirect('/')
else:
form = UserCreationForm()
return render(request, 'accounts/signup.html', {'form': form})
urls.py
File in the accounts
AppIf not already created, add a urls.py
file inside the accounts
app and include the following:
from django.urls import path
from .views import signup
urlpatterns = [
path('signup/', signup, name='signup'),
]
In your project’s urls.py
, include the accounts
app’s URLs:
from django.urls import path, include
urlpatterns = [
path('accounts/', include('accounts.urls')),
path('admin/', admin.site.urls),
]
Create a template to render the signup form.
Inside the accounts
app, create the directory structure: templates/accounts/
.
Create a file named signup.html
in templates/accounts/
with the following content:
<!DOCTYPE html>
<html>
<head>
<title>Signup</title>
</head>
<body>
<h1>Signup</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Signup</button>
</form>
</body>
</html>
By default, after signup, users are redirected to /accounts/profile/
. To change this behavior:
settings.py
Add the following line:
LOGIN_REDIRECT_URL = '/'
This will redirect users to the homepage after they sign up and log in.
Start the Django development server:
python manage.py runserver
Visit http://127.0.0.1:8000/accounts/signup/
to see the signup form. Register a new user and verify the flow.
The default UserCreationForm
provides basic fields: username, password, and password confirmation. To add more fields (e.g., email), create a custom form.
forms.py
Create a forms.py
file in your accounts
app and add the following:
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class CustomUserCreationForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2']
Update the signup
view in views.py
:
from .forms import CustomUserCreationForm
def signup(request):
if request.method == 'POST':
form = CustomUserCreationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect('/')
else:
form = CustomUserCreationForm()
return render(request, 'accounts/signup.html', {'form': form})
To enhance the design of the signup page, use Bootstrap. Replace the content of signup.html
with:
<!DOCTYPE html>
<html>
<head>
<title>Signup</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
</head>
<body class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<h2 class="text-center">Signup</h2>
<form method="post" class="mt-4">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary w-100">Signup</button>
</form>
</div>
</div>
</body>
</html>
With this guide, you’ve successfully implemented a signup feature in Django using the contrib.auth
library. The default UserCreationForm
provides a quick and secure way to handle user registration. For additional functionality, you can customize the form or add fields as needed.
Feel free to extend this setup with email verification, password resets, or social authentication using packages like django-allauth
.
Happy Coding! 🎉
Login Functionality in Django Using contrib.auth Copy Copy
December 10, 2024 | 0 Comments
December 10, 2024 | 0 Comments
December 10, 2024 | 0 Comments
SIP Trunking Services Comparison Copy Copy
December 10, 2024 | 0 Comments
Password Reset Functionality in Django Using contrib.auth Copy Copy
December 10, 2024 | 0 Comments
Web-to-Phone System Using Asterisk Copy Copy
December 10, 2024 | 0 Comments
We will zealously try to help you by providing technical support. We are open to inquiries or requests.
info@trickywebsolutions.com
+1-2252762741
+916280560026
1945 Brightside Drive, Baton Rouge, LA -70820
We are available for a friendly chat to discuss your business needs, no obligation.