Can't register a User using register function

Hey everyone !

I’m experiencing a weird issue with my register function :

views.py

    from django.shortcuts import render
    from .forms import SignUpForm, LoginForm

    def registerUser(request):
    if request.method == "POST":
        form = SignUpForm(request.POST)

        if form.is_valid():
            form.save()
            return redirect("/")

        else:
            print(form.errors)
    else:
        form = SignUpForm()

    template_name = "accounts/register.html"
    context = {
        "form":form,
    }

    return render(request, template_name, context)

forms.py

    from django.forms import ModelForm
    from django.contrib.auth.models import User
    from django.contrib.auth.forms import UserCreationForm

    class SignUpForm(UserCreationForm):
    class Meta:
        model = User
        fields = "__all__"

    class LoginForm(ModelForm):
    class Meta:
        model = User
        fields = "__all__"

register.html

    {% extends "base.html" %}

    {% block page_title %}Blog : Register{% endblock %}

    {% block page_content %}
    <form id="registerForm" action="{% url 'accounts:registerUser' %}" method="POST">
    {% csrf_token %}

    {{ form.username.label }}
    {{ form.username }}

    {{ form.email.label }}
    {{ form.email }}

    {{ form.password1.label }}
    {{ form.password1 }}

    {{ form.password2.label }}
    {{ form.password2 }}

    {{ form.errors }}

    <input type="submit" value="Sign Up">
    </form>
    {% endblock %}

When I click on the “Sign Up” button on register.html I get (whatever I try to fill in) :

password
This field is required.
date_joined
This field is required.

Does someone have a clue ?

Thanks !

You have a mismatch between the fields defined for the form and the form itself.

In your SignUpForm you specify that there will be Form fields for every field in the model. That includes the Model Fields for password and date_joined.

The simplest solution is to specify only the fields you need in the form definition.

Ken

1 Like

Thank you it worked perfectly !