DEV Community

Cover image for django UserCreation Form - fastest way to create signup functionality.
rahulshukla007
rahulshukla007

Posted on

django UserCreation Form - fastest way to create signup functionality.

The simplest way to create user in django is by using usercreation form.
Django comes with pre-built wheel so that the developer can focus solely on building web application.
For creating user registration we have to work on these files
1) views.py
2) urls.py
3) registration.html

1) views.py

from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm

def signUp(request):
    fm = UserCreationForm() 
    return render(request, 'account/registration.html', {'form':fm})
Enter fullscreen mode Exit fullscreen mode

2) urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.signUp, name= 'SignUp')
]
Enter fullscreen mode Exit fullscreen mode

3) html.py

<!DOCTYPE html>
<html>
<head>
<title>Registation</title>
</head>
<body>

{{form.as_p}}

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

4) we are using inbuilt auth application of django that's why we have to run migrations & migrate command

>>>python manage.py makemigrations
>>>python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

5) Now you can see the django inbuilt registration form.
The form will include three fields by default username, password1, and password2. The form contain inbuilt html code

Alt Text

6) Now its time to fill the signup form,
But before that we have to create a function inside views.py which handles post request coming from registration.html
views.py

def signUp(request):
    if request.method == "POST":
        #print(request.POST)
        fm = UserCreationForm(request.POST)
        if fm.is_valid():
            fm.save()
    else:
       fm = UserCreationForm()

    return render(request, 'account/registration.html', {'form':fm})
Enter fullscreen mode Exit fullscreen mode

7) The following method specifies how to send form-data
registration.html

<!DOCTYPE html>
<html>
<head>
<title>Registation</title>
</head>
<body>
<form action="", method="post"> 
   {% csrf_token %}
    {{form.as_p}}
    <input type="submit" value="Submit">
</form>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

8) Now we are ready to fill form with submit button

Alt Text

9) But before submitting once we go through the database, so you get some knowledge that how our data is going to save in our database
so for that download the sqlite browser through the link
https://sqlitebrowser.org/dl/
now it look like this -

Alt Text

10) Now click on file > open database >open your project directory db file

Alt Text

11) Now right click on auth user & browse the table

Alt Text

12)& Now you can see the table like this
Alt Text

13) Its time to submit the form
Alt Text

14) After submitting you can see the form value like this
Alt Text

15) below is the github repo link to fetch all the code we have
done so far

Alt Text

In my next post we will create customize registration form with some bootstrap UI

Top comments (0)