info@trickywebsolutions.com |
+1-225-276-2741
Tricky Websolutions is a part of our secret weapon. I constantly attribute part of our success to our relationship with them...
- Duncan Bell
Columbia Books & Information Services
Owing to our experience of working with Fortune 500 company products, we have been able to conceive the best of products that meet market demand
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. Step 1: Setting Up Your Django Project 1. Create a Project and App Run the following commands to create your project and app: django-admin startproject myprojectcd myprojectpython manage.py startapp accounts 2. Add […]
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.
django.contrib.auth
Run the following commands to create your project and app:
django-admin startproject myprojectcd myprojectpython manage.py startapp accounts
In settings.py, add the accounts app to INSTALLED_APPS:
settings.py
accounts
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, redirectfrom django.contrib.auth.forms import UserCreationFormfrom django.contrib.auth import logindef 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
If not already created, add a urls.py file inside the accounts app and include the following:
from django.urls import pathfrom .views import signupurlpatterns = [ path('signup/', signup, name='signup'),]
In your project’s urls.py, include the accounts app’s URLs:
from django.urls import path, includeurlpatterns = [ 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/.
templates/accounts/
Create a file named signup.html in templates/accounts/ with the following content:
signup.html
<!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:
/accounts/profile/
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.
http://127.0.0.1:8000/accounts/signup/
The default UserCreationForm provides basic fields: username, password, and password confirmation. To add more fields (e.g., email), create a custom form.
UserCreationForm
forms.py
Create a forms.py file in your accounts app and add the following:
from django import formsfrom django.contrib.auth.models import Userfrom django.contrib.auth.forms import UserCreationFormclass CustomUserCreationForm(UserCreationForm): email = forms.EmailField(required=True) class Meta: model = User fields = ['username', 'email', 'password1', 'password2']
Update the signup view in views.py:
signup
from .forms import CustomUserCreationFormdef 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.
contrib.auth
Feel free to extend this setup with email verification, password resets, or social authentication using packages like django-allauth.
django-allauth
Happy Coding! 🎉
Login Functionality in Django Using contrib.auth Copy Copy
December 10, 2024 | 0 Comments
Web Development Copy Copy
SIP Trunking Services Comparison Copy Copy
Password Reset Functionality in Django Using contrib.auth Copy Copy
Web-to-Phone System Using Asterisk Copy Copy
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.