# IntegrityError: NOT NULL constraint failed: usuarios\_usuario.email

**URL:** <https://forum.djangoproject.com/t/integrityerror-not-null-constraint-failed-usuarios-usuario-email/35890>\
**Category:** Mystery Errors\
**Created:** [October 23, 2024, 1:52pm UTC](https://forum.djangoproject.com/t/integrityerror-not-null-constraint-failed-usuarios-usuario-email/35890 "2024-10-23T13:52:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hbarreto1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hbarreto1/32/24746_2.png) [@Hbarreto1](https://forum.djangoproject.com/u/Hbarreto1)\
**Post date:** [October 23, 2024, 1:52pm UTC](https://forum.djangoproject.com/t/integrityerror-not-null-constraint-failed-usuarios-usuario-email/35890/1 "2024-10-23T13:52:49Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/6/2/62273215831b2ecaef3081996d21084cdbc259dc.png)  
the error is in a diferent language because this text is translated for the english. But the code is right.

**Models:**

```auto
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:**

```auto
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**

```auto
<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>

```

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [October 24, 2024, 12:07am UTC](https://forum.djangoproject.com/t/integrityerror-not-null-constraint-failed-usuarios-usuario-email/35890/2 "2024-10-24T00:07:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hbarreto1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hbarreto1/32/24746_2.png) [@Hbarreto1](https://forum.djangoproject.com/u/Hbarreto1)\
**Post date:** [October 24, 2024, 10:59am UTC](https://forum.djangoproject.com/t/integrityerror-not-null-constraint-failed-usuarios-usuario-email/35890/3 "2024-10-24T10:59:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [October 24, 2024, 11:13am UTC](https://forum.djangoproject.com/t/integrityerror-not-null-constraint-failed-usuarios-usuario-email/35890/4 "2024-10-24T11:13:01Z")

</div>

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:

> [@Hbarreto1](#):
>
> `email=form.get('email')`

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.

---

<div class="post-metadata">

**Author:** ![Hbarreto1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/hbarreto1/32/24746_2.png) [@Hbarreto1](https://forum.djangoproject.com/u/Hbarreto1)\
**Post date:** [October 24, 2024, 11:20am UTC](https://forum.djangoproject.com/t/integrityerror-not-null-constraint-failed-usuarios-usuario-email/35890/5 "2024-10-24T11:20:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [October 24, 2024, 11:34am UTC](https://forum.djangoproject.com/t/integrityerror-not-null-constraint-failed-usuarios-usuario-email/35890/6 "2024-10-24T11:34:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![qvisty](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/qvisty/32/4818_2.png) [@qvisty](https://forum.djangoproject.com/u/qvisty)\
**Post date:** [October 24, 2024, 3:59pm UTC](https://forum.djangoproject.com/t/integrityerror-not-null-constraint-failed-usuarios-usuario-email/35890/7 "2024-10-24T15:59:35Z")

</div>

> [@leandrodesouzadev](#):
>
> 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

Could you please elaborate in this context?

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [October 24, 2024, 4:14pm UTC](https://forum.djangoproject.com/t/integrityerror-not-null-constraint-failed-usuarios-usuario-email/35890/8 "2024-10-24T16:14:39Z")

</div>

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`.
