我刚开始学习Django。虽然我正在尝试创建一个非常简单的用户注册函数(使用 PSQL),但我总是收到以下消息:
完整性错误:“电子邮件”列中的空值违反了非空约束我认为我的数据库有问题。但我想不通为什么..
我正在使用 Django 1.5.1 和 PSQL 9.1.9 非常感谢!!
模型.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)
视图.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})
追溯:
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
我检查了我的数据库,我看到有一个名为 porfolioSite_user 的表,但是当我删除它时,psql 告诉我该表没有退出......所以很困惑......