Loading image

Blogs / Programming

How to Create a Custom Signup Form in Django 5.0

How to Create a Custom Signup Form in Django 5.0

  • Qadir Hassan
  • 0 Comments
  • 660 View

If you're developing a web application using Django, you'll probably need to construct a custom signup form for your users. While Django has an integrated authentication mechanism and a default form for user registration, it may not always meet your requirements. In this article, we will walk you through the process of designing a custom signup form in Django.

Step 1: Create a new Django app

The first step is to create a new Django app that includes our customized signup form. Open your terminal and navigate to the directory where you intend to create the new app. Then, execute the following command:

python manage.py startapp userauths

Step 2: Define the Custom User Model

The next step is to define the User model for our own signup form. Django includes a built-in User model, but we will develop our own to add new fields as needed.

In your userauths/models.py file, include the following code:

from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.


class User(AbstractUser):
    email = models.EmailField(unique=True)
    username = models.CharField(max_length=100)
    bio = models.TextField(max_length=300)

    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = ['username',]

    def __str__(self):
        return self.username

Step 3: Create the signup form

Now we'll create the actual signup form. In  userauths/forms.py file, add the following code:

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

class SignupForm(UserCreationForm):
    email = forms.EmailField(max_length=200, help_text='Required')

    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2')

This code creates a new SignupForm that inherits from Django's built-in UserCreationForm. We've created an email field and indicated which fields we want to be included in the form.

Step 4: Configure the view

Next, we'll set up the view that will handle the signup form. In your userauths/views.py file, include the following code:

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

def signup(request):
    if request.method == 'POST':
        form = SignupForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect('home')
    else:
        form = SignupForm()
    return render(request, 'signup.html', {'form': form})

Step 5: Create the template

The last step is to draft a template for our registration form. Make a new file named "signup.html" in your templates/accounts directory, and add the following code to it:

<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Sign up</button>
</form>

With the help of this code, a simple HTML form with all the fields specified in our SignupForm is created. For security, a CSRF token is also included.

Conclusion

You've successfully used Django to construct a personalized sign-up form. Now that you know this, you can further tailor your authentication system to meet your unique requirements.

Check out my profile at @Linkedin/qadir-hassan if you need assistance creating unique web applications or if you're seeking remote Python developers to join your team.

FAQs

Q1. What is Django?

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.

Q2. What are Django forms?

Django forms provide a way to handle user input and validation in your web application. They simplify creating HTML forms, validating submitted data, and processing it.

Q3. What are Django forms?

You can manage user input and validation in your web application with Django forms. They make the process of building HTML forms, validating data entered, and processing it easier.


  • Programming
Qadir Hassan Author

Qadir Hassan

0 Comments

Post Comment

Recent Blogs

Recent posts form our Blog

how to install node js in shared hosting server

how to install node js in shared hosting server

showkat ali
/

Read More
An Innovative Method to Building Online Urdu Corpus (اردو کارپس)

An Innovative Method to Building Online Urdu Corpus (اردو کارپس)

Nasir Hussain
/
English

Read More
How To Integrate Paypal Payment Gateway In Laravel 10

How To Integrate Paypal Payment Gateway In Laravel 10

showkat ali
/
Programming

Read More
How to pitch in ChatGPT in Education

How to pitch in ChatGPT in Education

Nasir Hussain
/
English

Read More
How to Deploy a Next.js Application on AWS EC2 Using GitHub Actions

How to Deploy a Next.js Application on AWS EC2 Using GitHub Actions

showkat ali
/
Programming

Read More
Five Steps Sample Lesson Plan for English Grade 5th, 6th, 7th 8th, 9th, and 10th

Five Steps Sample Lesson Plan for English Grade 5th, 6th, 7th 8th, 9th, and 10th

Nasir Hussain
/
English

Read More