Reputation: 73
I just start to learn Django. While I'm trying to create a very simple user register function(with PSQL),but I always got this message:
Integrity error: null value in column "email" violates not-null constraint I think there is something wrong with my database. But I couldn't figure it why..
I'm using Django 1.5.1 and PSQL 9.1.9 THANK YOU VERY MUCH!!
models.py:
from django.db import models
class User(models.Model):
name = models.CharField(max_length=30)
email = models.EmailField(verbose_name='e-mail')
password = models.CharField(max_length=16)
views.py:
from portfolioSite.forms import register_form
from django.shortcuts import render
from django.http import HttpResponseRedirect
from portfolioSite.models import User
def register(request):
if request.method == 'POST':
registerForm = register_form(request.POST)
if registerForm.is_valid():
User.objects.create(name = registerForm.cleaned_data['name'],
email = registerForm.cleaned_data['email'],
password = registerForm.cleaned_data['password2'])
return HttpResponseRedirect('/success/')
else:
registerForm = register_form()
return render(request, 'register_form.html', {'form':registerForm})
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/home/guangyi/Django/projects/mysite/portfolioSite/views.py" in register
13. password = registerForm.cleaned_data['password2']
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in create
149. return self.get_query_set().create(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in create
402. obj.save(force_insert=True, using=self.db)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save
546. force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
650. result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py" in _insert
215. return insert_query(self.model, objs, fields, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in insert_query
1661. return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py" in execute_sql
937. cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py" in execute
41. return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py" in execute
56. six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py" in execute
54. return self.cursor.execute(query, args)
Exception Type: IntegrityError at /register/
Exception Value: null value in column "email" violates not-null constraint
I check my db, I saw there is a table called porfolioSite_user, but when I drop it, psql told me the table does not exit...so confused....
Upvotes: 3
Views: 19089
Reputation: 500
Check your template to make sure the email field in your form is submitting the 'email
' input correctly. If it's submitting nothing under 'email
' when you hit submit, that's the likely cause of your problem.
There are a number of registration apps that you can install that will get rid of the trouble you're having with this. I recommend django-registration-1.5
since youre using a version the version of django with customizable user models. It would be the simplest way to go, but in case you're doing this for self-enrichment, Theres a number of other things you can try if the form appears ok. You can check your south migrations to make sure that they're all up to date, or if you're in testing phases and your database doesn't matter, you can flush the database to see if it gets rid of the error.
model fields have two similar options, but do different things. They are the 'null=True
' value and the 'blank=True
' one. You only have 'blank=True
', which is the validator for the python side of creating the object. However, when you try to save it to the database, you'll get an error, and thats what's most likely happening here. Since your 'email
' model is missing the the 'null=True
' option on it, you get an error because you're submitting a 'null
' value to a not-null constraint. However, adding 'null=True
' to your model is not the right thing to do in this situation.
if you want to save yourself a headache, I suggest plugging django-registration-1.5
into your project and letting that app do the registration for you.
Upvotes: 3
Reputation: 174662
By default all fields in a django model are required and do not accept null values. In order for a field to accept nulls, you need to add null=True
to the model field definition.
The problem in your case is that the form does not validate that an email is required; so it is accepting blanks in the email field.
Another issue is that you are using a model called User
when django already comes with a complete authentication system with users, permissions and - yes - even the registration and forgot password forms (see more on this in the documentation). This application provides a User
model as well.
Finally, django will never make any destructive changes to your database. This means if you delete a model, or delete add a column to a model the syncdb
command will not make these changes. You will have to drop the database and then run syncdb
again. Later on you can learn about applications like South
that help keep track of these changes and do the database modifications for you.
Upvotes: 1
Reputation: 790
Try to check the email field before you send a POST request, make sure the email field is not NULL before you send the request.
Upvotes: 0