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
  • 851 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 NVIDIA’s AI Technologies Can Accelerate Your Deep Learning, Data Science, and Edge AI Projects

How NVIDIA’s AI Technologies Can Accelerate Your Deep Learning, Data Science, and Edge AI Projects

showkat ali
/
Technology

Read More
Character AI: An Overview and How It Could Affect the World

Character AI: An Overview and How It Could Affect the World

showkat ali
/
Programming

Read More
Using Multiple Select as Count in the Select2 Search Box

Using Multiple Select as Count in the Select2 Search Box

showkat ali
/
Programming

Read More
The Ultimate Guide to Data Structures: Understanding, Applications, and Best Practices

The Ultimate Guide to Data Structures: Understanding, Applications, and Best Practices

showkat ali
/
Programming

Read More
Build and Deploy Your Laravel Application Using GitHub Actions

Build and Deploy Your Laravel Application Using GitHub Actions

showkat ali
/
Programming

Read More
Integrate Twilio in Laravel: Made Easy Way

Integrate Twilio in Laravel: Made Easy Way

showkat ali
/
Programming

Read More