IntegrityError: NOT NULL constraint failed: usuarios_usuario.email

I am encountering an error when trying to register a user in my database. The user table I am using is not the default Django user model.

The error is:
image
the error is in a diferent language because this text is translated for the english. But the code is right.

Models:

class User(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField(unique=True)
    password = models.CharField(max_length=50)
    creation_date = models.DateTimeField(auto_now_add=True)
    admin = models.BooleanField(default=False)

    def __str__(self):
        return self.name

Views:

def register(request):
    if request.method == 'POST':
        form = request.POST
        if form is not None:
            if User.objects.filter(email=form.get('email')).exists():
                return render(request, 'register.html', {'error': 'Email already in use'})
            new_user = User.objects.create(name=form.get('name'), email=form.get('email'), password=form.get('password'), creation_date=date.today(), admin=False)
            new_user.save()
            return redirect('login')
    else:
        return render(request, 'register.html')

HTML

<form method='POST' action="{% url 'register' %}">
    {% csrf_token %}
    <p>Create your account</p>

    <div data-mdb-input-init class="form-outline mb-4">
        <input type="text" class='form-control' id='name' name="name" placeholder="Name" pattern="^[a-zA-Z][a-zA-Z-_\.]{3,20}$" required>
        <label class="form-label" for="name">Name</label>
    </div>

    <div data-mdb-input-init class="form-outline mb-4">
        <span class='text-danger' id='msgEmail'></span>
        <input type="email" id="email" class="form-control" placeholder="Email" name="email" />
        <label class="form-label" for="email">Email</label>
    </div>

    <div data-mdb-input-init class="form-outline mb-4">
        <span class='text-danger' id='message'></span>
        <input type="password" id="password" class="form-control" name="password" />
        <label class="form-label" for="password">Password</label>
    </div>

    <div data-mdb-input-init class="form-outline mb-4">
        <span class='text-danger' id='messagePassword2'></span>
        <input type="password" id="confirmPassword" class="form-control" name="confirmPassword" />
        <label class="form-label" for="confirmPassword">Confirm Password</label>
    </div>

    <div class="text-center pt-1 mb-5 pb-1">
        <button id='button' data-mdb-button-init data-mdb-ripple-init class="btn btn-primary btn-block fa-lg gradient-custom-2 mb-3" type="submit">Register</button>
    </div>
</form>

check request.POST.get(‘email’) before create user.

Hi, after checked, here is the response
Uploading: imagem_2024-10-24_075718286.png…
print(email) returns None.

Hello there!
It looks like your image is not being loaded correctly, that’s also good. When you receive an error, is better for you to post the traceback instead of a image. This can help other people to find a issue similar to the one you’re facing.

Even without the traceback, or the response data. It’s clear to me that this line is returning a None value:

You really should have been using a form on your view, even if you’re not using that form to render the HTML. You’re missing a lot of goodies that the django.forms.Form gives to you.

Now, to the debugging part, from the traceback page, you can search for a field called ‘POST DATA’ there you can view all the data that was sent with this request. That should give you a hint of where is the problem.

Hello, basically the return for print(email) was None. Thanks for the tip about django.Form

Good to know that you find out.
Even though print can help you hack your way into debugging quickly, a good skill to know is to use a debugger. If you’re using vscode, there’s a almost ready configuration for you to use. It makes thing real easier.

Could you please elaborate in this context?

Using a form is better for the data-validation. Using request.POST.get you only get the raw data.
You can be declarative with the fields and how centralize the data validation there.
And on your view, all you have to do is create an instance of that form with the request.POST and you can check if the data is_valid.