1

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"]

register.html

{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block contain %}
    <h1>Register</h1>
    <form method="POST" class="form-group">
        {% csrf_token %}
    {{forms|crispy}}
    <button type="submit" class="btn btn-success">Register</button>
    </form>

{% endblock%}

4
  • Do you get any errorrs? What debugger is shopwing? What have you tried already? Commented Oct 1, 2022 at 16:14
  • Can you add the HTML of the code? Commented Oct 1, 2022 at 16:15
  • {% extends 'base.html' %} {% load crispy_forms_tags %} {% block contain %} <h1>Register</h1> <form method="POST" class="form-group"> {% csrf_token %} {{forms|crispy}} <button type="submit" class="btn btn-success">Register</button> </form> {% endblock%}
    – Noob_Gamer
    Commented Oct 1, 2022 at 16:15
  • @BartoszStasiak I am not getting any errors
    – Noob_Gamer
    Commented Oct 1, 2022 at 16:18

1 Answer 1

1

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'),
]

Not the answer you're looking for? Browse other questions tagged or ask your own question.