• OUR GLOBAL PRESENCE:
  • USA
  • IN
  • SG
  • PT

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 myproject
cd myproject
python manage.py startapp accounts

2. Add the App to Installed Apps

In settings.py, add the accounts app to INSTALLED_APPS:

INSTALLED_APPS = [
...,
'accounts',
'django.contrib.auth',
'django.contrib.contenttypes',
...,
]

3. Apply Migrations

Run the following command to ensure all required database tables are created:

python manage.py migrate

Step 2: Creating a Signup View

To enable user registration, we need a signup form and a view.

1. Create a Signup View in 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})

Step 3: Adding URLs for Signup

1. Create a urls.py File in the accounts App

If 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'),
]

2. Include the App URLs in the Project URLs

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),
]

Step 4: Creating the Signup Template

Create a template to render the signup form.

1. Create a Template Directory

Inside the accounts app, create the directory structure: templates/accounts/.

2. Add a Signup Template

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>

Step 5: Redirecting After Signup

By default, after signup, users are redirected to /accounts/profile/. To change this behavior:

1. Update settings.py

Add the following line:

LOGIN_REDIRECT_URL = '/'

This will redirect users to the homepage after they sign up and log in.


Step 6: Testing the Signup Flow

1. Run the Development Server

Start the Django development server:

python manage.py runserver

2. Access the Signup Page

Visit http://127.0.0.1:8000/accounts/signup/ to see the signup form. Register a new user and verify the flow.


Step 7: Customizing the Signup Form (Optional)

The default UserCreationForm provides basic fields: username, password, and password confirmation. To add more fields (e.g., email), create a custom form.

1. Create a Custom Signup Form in 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']

2. Use the Custom Form in the View

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})

Step 8: Styling the Signup Page (Optional)

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>

Final Words

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! 🎉

We will zealously try to help you by providing technical support. We are open to inquiries or requests.

+1-2252762741

+916280560026

1945 Brightside Drive, Baton Rouge, LA -70820

Contact Us

Get in touch!

We are available for a friendly chat to discuss your business needs, no obligation.

Drop a message here, and we will get back to you soon.