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 manage.py startapp core

Step 3: Update setting.py

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

settings.py
....
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
]

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/views.py file and add:

core/views.py
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():
            form.save()
            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')
    else:
        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:

core/signup.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
    <style type="text/css">
        body{
            background-color: #f7fcff;
        }
    </style>
</head>
<body>
    <h3>How to Create SignUp View in Django? - Tuts-Station.com</h3>
    <form method="post">
        {% csrf_token %}
        {% for field in form %}
        <p>
            {{ 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 %}
        </p>
        {% endfor %}
    <button type="submit">Sign up</button>
  </form>
</body>
</html>

Step 6: Creating URLs

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

core/urls.py
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.

example/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    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 manage.py runserver

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

I Hope It will help you....