How to create more than one user SignUp in Django?

We have often visited websites such as school websites, banking websites and so on, where there are different signup forms for different users. We came across some of our projects in which we wonder how to create more than one user signup in django ?, because in Django we know that there is only one auth user.

In this blog, you are going to get all your questions answered and also, by the end of this blog you will be able to create your own more than one user login.

Create-more-than-one-signup

So, we can create more than one user login with the help of our auth user model. Yes you are reading it right, Django provides a flexibility to extend the AbstractUser model and switch the AUTH_USER_MODEL on your settings. With the help of this, you can freely add custom methods to the user model without depending on OneToOne model.

Abstract user model behave similar to the user model, with the freedom that you can customize it accordingly. Here, we are going to extend the abstract model and add our own fields.

Creating More than one user SignUp

Step1:- Create django project folder

  1. Create django project folder
django admin startproject blog_more_user.
  1. Create app  in the django project folder.
python manage.py startapp user

3. In settings.py add app name.

Settings.py

INSTALLED_APPS = [
    'user',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Step2:- Add files and folder in django project folder

We need to create a template folder in the django folder and a urls.py file in the app folder.

  1. Create a new folder in the django folder(here, blog_more_user folder) save it with the name template.

2. Add the path for this template folder in blog_more_user > settings.py .

Settings.py

import os
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR,'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

3. Create a new file in the app folder(here, user) save it with the name urls.py

4. Add path for this url.py file in blog_more_user > urls.py .

Urls.py

from django.contrib import admin
from django.urls import path,include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('user.urls'))
]

Step3:- Create more than one user

We are first going to add our database to our project. In settings.py add the below code according to your database in DATABASES.

  1. Add database to our django project folder.

Settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME':'newuser',
        'USER': 'postgres',
        'PASSWORD':"2320",
        'HOST': 'localhost'
    }
}

2. Create model

Now we will create our model for which we want to have more than one user signup. We will extend the AbstractUser model and create a new class name as User and going to add fields is_customer and is_manager to know that when the user signup he is registering as customer or manager.

Models.py

from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
    is_customer=models.BooleanField(default=False)
    is_manager=models.BooleanField(default=False)
class Manager(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)
    name = models.CharField(max_length=100)
    age = models.CharField(max_length=100)   
class Customer(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True)
    name = models.CharField(max_length=100)
    age = models.CharField(max_length=100)

In settings.py add the below code, otherwise you will face error.

Settings.py

AUTH_USER_MODEL='user.User'

3. Migrate the model.

Run the below code to migrate your model into your database.

 python manage.py makemigrations

Instead of user type your app name

python manage.py sqlmigrate user 0001
Python manage.py migrate

4. Create form

After the model we need to create forms for our model here, we are using UserCreationForm which will provide us the default form of django.

Forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.db import transaction
from .models import Customer, Manager,User
class CustomerSignUpForm(UserCreationForm):
    name=forms.CharField(required=True)
    age=forms.CharField(required=True)
    class Meta(UserCreationForm.Meta):
        model = User
    @transaction.atomic
    def save(self):
        user = super().save(commit=False)
        
        user.is_customer = True
        user.save()
        student = Customer.objects.create(user=user)
        return user
class ManagerSignUpForm(UserCreationForm):
    name=forms.CharField(required=True)
    age=forms.CharField(required=True)
  
    class Meta(UserCreationForm.Meta):
        model = User
 
    @transaction.atomic
    def save(self):
        user = super().save(commit=False)
        
        user.is_manager = True
        user.save()
        manager = Manager.objects.create(user=user)
        
        manager.save()
 
        return manager

5. Create index html page

Index.html

<html>
<head>
    <title>
        More than one user
    </title>
</head>
<body>
    <button><a href="{% url 'customer_signup' %}">Regiter as Customer</a></button><br><br>
    <button><a href="{% url 'manager_signup'%}">Regiter as Manager</a></button>
</body>
</html>

6. Create signup html page

Signup.html

<html>
 <head>
    <title>
        More than one user
    </title>
</head>
<body>
    <form method="POST" accept-charset="utf-8">
        {% csrf_token %}
        {{form.as_p}}
        <input type="Submit" name="submit" value="Signup">
    </form>
</body>
</html>

7. Add path for the pages and actions in user(app_name) > urls.py.

Urls.py

from django.urls import path
from .views import *
urlpatterns = [
    path('', index,name='index'),
    path('customer', CustomerSignUpView.as_view(), name='customer_signup'),
    path('manager', ManagerSignUpView.as_view(), name='manager_signup'),
]

8. Add function in our user(app_name) > views.py.

Views.py

from django.shortcuts import render
from django.shortcuts import redirect
from .models import Customer,Manager,User
from django.views.generic import CreateView
from .forms import CustomerSignUpForm,ManagerSignUpForm
# Create your views here.
def index(request):
    return render(request,'index.html')
class CustomerSignUpView(CreateView):
    model = User
    form_class = CustomerSignUpForm
    template_name = 'signup.html'
    def get_context_data(self, **kwargs):
        kwargs['user_type'] = 'customer'
        return super().get_context_data(**kwargs)
    def form_valid(self, form):
        user = form.save()
        login(self.request, user)
        return redirect('index')
class ManagerSignUpView(CreateView):
    model = User
    form_class = ManagerSignUpForm
    template_name = 'signup.html'
    def get_context_data(self, **kwargs):
        kwargs['user_type'] = 'manager'
        return super().get_context_data(**kwargs)
    def form_valid(self, form):
        user = form.save()
        #login(self.request, user)
        return redirect('index')

Output :-

Button for signing up as customer or manager
Signup page

Quick Revision

  1. Create your django project folder.
  2. Make new app in the django project folder.
  3. Add template folder in the django folder and urls.py in app(your_app_name) > urls.py.
  4. Add the database to your project folder.
  5. Create a model in app(your app_name ) > forms.py.
  6. Add form for that model in app(your app_name) > forms.py.
  7. Create index html page and signup html page.
  8. Add path for the action in app(your app_name) > urls.py.
  9. Provide function for the action in app(your app_name) > forms.py.

GitHub link :-

https://github.com/jaya2320/blog_more_user