How to Create SignUp View in Django?

Published On: 26/11/2022 | Category: Django

Hi Dev,

Today, how to create signup view in django is our main topic. I would like to share with you how to create signup and registration view in django. I explained simply about how to create custom registration form in django. if you want to see example of how to create signup in django then you are a right place. You just need to some step to done how to create a signup page in django.

The most straightforward way to implement a user sign up is to use the UserCreationForm as is. This strategy is appropriate if you are using the default Django user, authenticating with username, and are only interested in setting the username and password upon sign up.

Here i explained simply step by step example of how to create signup view in django.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example

Step 2: Create a App

Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python3 startapp core

Step 3: Update

Then update INSTALLED_APPS within our file to notify Django about the app.


Step 4: Creating the Views

In this step, we need to configure views. The remove_bg page will just be a template. function is basically remove background in original image the open the core/ file and add:

from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect

# Create View
def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('home')
        form = UserCreationForm()
    return render(request, 'signup.html', {'form': form})

Step 5: Creating Templates

Next, then with your text editor create new templates files: core/signup.html file and the add:

<!DOCTYPE html>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style type="text/css">
            background-color: #f7fcff;
    <h3>How to Create SignUp View in Django? -</h3>
    <form method="post">
        {% csrf_token %}
        {% for field in form %}
            {{ field.label_tag }}<br>
            {{ field }}
            {% if field.help_text %}
                <small style="color: grey">{{ field.help_text }}</small>
            {% endif %}
            {% for error in field.errors %}
                <p style="color: red">{{ error }}</p>
            {% endfor %}
        {% endfor %}
    <button type="submit">Sign up</button>

Step 6: Creating URLs

In this section, we need a file within the core app however Django doesn't create one for us with the startapp command. Create core/ with your text editor and paste below code.

from django.urls import path, re_path
from . import views

urlpatterns = [
    re_path(r'^signup/$', views.signup, name='signup'),

Next, we require to add a URL path for our example app which can be done by importing include and setting a path for it.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('core.urls')),

Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the http://localhost:8000/signup address with a web browser.

I Hope It will help you....