Can't register a User using register function

Hey everyone !

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

    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():
            return redirect("/")

        form = SignUpForm()

    template_name = "accounts/register.html"
    context = {

    return render(request, template_name, context)

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


    {% 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.password1.label }}
    {{ form.password1 }}

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

    {{ form.errors }}

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

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

This field is required.
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.


1 Like

Thank you it worked perfectly !