In this tutorial, we will learn how to create ( Django signup view)Sign Up or Registration Form in Django for User Registration. We will be using UserCreationForm which is an in-built form for User Registration. User Sign Up can be created by the third person or by self. Both strategics can be achieved by this tutorial. You can get the working application of this tutorial on GitHub.

For example, we will be using the project in which we have created previously a login/logout and password reset/change view. For this tutorial, I have created an app named core.

Here you will be learning following example –

Let’s get started.

Django User Registration Authentication – SignUpView

We can implement simple sign up registration by using in-built UserCreationForm Auth Form(signup class based view). We will be using CreateView in View. We can create sign up using only username and password. But we are adding extra fields in forms.py ( django create user form ) while registration like first last name and email.

urls.py

from django.urls import path
from core.views import SignUpView

urlpatterns = [
    path('signup/', SignUpView.as_view(), name='signup'),
]

views.py

from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView
from core.forms import SignUpForm

# Sign Up View
class SignUpView(CreateView):
    form_class = SignUpForm
    success_url = reverse_lazy('login')
    template_name = 'commons/signup.html'

forms.py

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

# Sign Up Form
class SignUpForm(UserCreationForm):
    first_name = forms.CharField(max_length=30, required=False, help_text='Optional')
    last_name = forms.CharField(max_length=30, required=False, help_text='Optional')
    email = forms.EmailField(max_length=254, help_text='Enter a valid email address')

    class Meta:
        model = User
        fields = [
            'username', 
            'first_name', 
            'last_name', 
            'email', 
            'password1', 
            'password2', 
            ]

signup.html

{% extends 'base.html' %}

{% block title %}Sign Page{% endblock title %}

{% block content %} 
    <h2>Sign Page</h2>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Register</button>
        <br><br>
        <a href="{% url 'home' %}">Home</a>
    </form>
{% endblock content %}

It will look this

Profile Update View Form After Sign Up in Django

As we have made a signup module, so it is obvious we are going to need a profile update module. For Profile Update Functionality, we are going to use generic UpdateView. These are the in-built class-based views that make our code DRY. We are going to use the User Model which we have used above.

views.py

from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView, UpdateView
from core.forms import SignUpForm, ProfileForm
from django.contrib.auth.models import User

# Edit Profile View
class ProfileView(UpdateView):
    model = User
    form_class = ProfileForm
    success_url = reverse_lazy('home')
    template_name = 'commons/profile.html'

forms.py

from django import forms
from django.contrib.auth.models import User

# Profile Form
class ProfileForm(forms.ModelForm):

    class Meta:
        model = User
        fields = [
            'username',
            'first_name', 
            'last_name', 
            'email',
            ]

urls.py

from django.urls import path
from core.views import SignUpView, ProfileView

urlpatterns = [
    path('signup/', SignUpView.as_view(), name='signup'),
    path('profile/<int:pk>/', ProfileView.as_view(), name='profile'),
]

profile.html

{% extends 'base.html' %}

{% block title %}Profile Page{% endblock title %}

{% block content %} 
    <h2>Profile Page</h2>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Update</button>
        <br><br>
        <a href="{% url 'home' %}">Back</a>
    </form>
{% endblock content %}

It will look like this –

Profile Update View After Sign Up Django

GitHub – Run Example Locally

Here is Django user registration and login example GitHub.Code is also available on GitHub – https://github.com/studygyaan/How-to-use-Built-In-Login-and-Logout-Authentication-System-in-Django

Clone the Repository

git clone https://github.com/studygyaan/How-to-use-Built-In-Login-and-Logout-Authentication-System-in-Django.git

Change Directory

cd How-to-use-Built-In-Login-and-Logout-Authentication-System-in-Django

Create Virtual Environment – VirtualENV

virtualenv env

Activate Virtual Environment

source env/bin/activate

Run requirements file to install libraries using Pip3

pip3 install -r requirements.txt

Run the server

python3 manage.py runserver

And open http://localhost:8000/ in your browser.