django UserCreation Form - fastest way to create signup functionality.
Tags: django, python
Published: 2020-12-10 16:46:53

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

<b>1) views.py</b>
```
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})
```
<b>2) urls.py</b>
```
from django.urls import path
from . import views

urlpatterns = [
    path('', views.signUp, name= 'SignUp')
]
```
<b>3) html.py</b>
```
<!DOCTYPE html>
<html>
<head>
<title>Registation</title>
</head>
<body>

{{form.as_p}}

</body>
</html>
```

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
```
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](https://dev-to-uploads.s3.amazonaws.com/i/lnx3rddqal4uet9l2jy7.png)

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
<b> views.py</b>
```
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})
```
7) The following method specifies how to send form-data 
<b> registration.html<b/>
```
<!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>
```
8) Now we are ready to fill form with submit button

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/roc6ep7if7nofhasmxeu.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/zi7ont5g6whnjtz1262s.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/leki822uc7qfrj161sgw.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b2jsxcidjnpsahfqgj0r.png)

12)& Now you can see the table like this
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/x8kjqrwnpg582sci4e4o.png)

13) Its time to submit the form
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/apvtcpr51bucs945sswa.png)

14) After submitting you can see the form value like this
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0brkzpt491qxf0ygocaf.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8avlqz3srz4gby6l43fk.png)



{% github https://github.com/rahulshukla007/django_crm %}

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