I cannot create the new user in django

I cannot create any new user from this method redirect is working and every thing is working but new user is not creating
this id my view

from urllib import response
from django.shortcuts import redirect, render
from .forms import RegisterForm


from main.views import home
# Create your views here.
def register(request):
    form = RegisterForm(request.POST or None)
    if request.method == "POST":
        
        if form.is_valid():
            form.save()
            return redirect("home")

        else:
            form = RegisterForm()
            
    return render(request, "register.html", {"forms":form})

Forms.py

from dataclasses import field
from socket import fromshare
from xml.parsers.expat import model
from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User

class RegisterForm(UserCreationForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ["username","email","password1","password2"]

I did it without crispy because I didn't work with it. I also removed base.html from the template because I don't have access to it. My users are saved. I think the problem is in this line:

else:
            form = RegisterForm()

If the type is POST, then the user sent the data. If a request of the GET type came in, then we create an empty form, followed by render(the form is shown for the first time):

else:
        form = RegisterForm()
        return render(request, "bboard/register.html", {"form": form})

Also note that

form = RegisterForm(request.POST or None)

a check is requested after:

if request.method == "POST":

In the line return render(request, "bboard/register.html", {"form": form}), replace bboard with the name of the folder where you have the templates. I have them in the templates folder, in which the bboard folder contains templates.

views.py

def register(request):
    if request.method == "POST":
        form = RegisterForm(request.POST or None)
        if form.is_valid():
            form.save()
            return redirect("register")
        else:
            return HttpResponse("Invalid data")
    else:
        form = RegisterForm()
        return render(request, "bboard/register.html", {"form": form})

templates

<h1>Register</h1>
<form method="post">
      {% csrf_token %}
      {{ form.as_p }}
      <input type="submit" value="adding">
</form>

urls.py

urlpatterns = [  
path('register/', register, name='register'),
]
Back to Top