我有一个简单的注册表格。如果人们可以填写信息,当他们提交表格时,数据应该放在数据库中。
应用程序在我的localhost中正常运行。我的表格中有一些必填字段。当我在我的localhost上运行应用程序并尝试提交表单而不填写必填字段时,它会显示一个弹出窗口并说明**该字段不能为空**,但当我将其部署在我的天蓝色门户网站上时尝试保存此而不填写必填字段然后它会生成错误:
ValueError at /register/
The UserInformation could not be created because the data didn't validate.
Request Method: POST
Request URL: http://pythonfabric.azurewebsites.net/register/
Django Version: 1.9.4
Exception Type: ValueError
Exception Value:
The UserInformation could not be created because the data didn't validate.
Exception Location: D:\home\site\wwwroot\env\Lib\site- packages\django\forms\models.py in save, line 446
Python Executable: D:\Python27\python.exe
Python Version: 2.7.8
Python Path:
[u'D:\\home\\site\\wwwroot\\env\\Lib\\site-packages',
'.',
'D:\\Windows\\SYSTEM32\\python27.zip',
'D:\\Python27\\DLLs',
'D:\\Python27\\lib',
'D:\\Python27\\lib\\plat-win',
'D:\\Python27\\lib\\lib-tk',
'D:\\Python27',
'D:\\Python27\\lib\\site-packages',
'D:\\home\\site\\wwwroot']
Server time: Thu, 9 Feb 2017 19:17:07 +0000
register.html
from django import forms
from django.forms import ModelForm
from django.contrib.auth.models import User
from app.models import UserInformation
from django.utils.translation import ugettext_lazy as _
class UserInformationForm(ModelForm):
firstName = forms.CharField(max_length=254,
widget=forms.TextInput({
'class': 'form-control',
}))
lastName = forms.CharField(
widget=forms.TextInput({
'class': 'form-control',
}))
emailAddress = forms.EmailField(
widget=forms.TextInput({
'class': 'form-control',
}))
phoneNumber = forms.CharField( required=False,
widget=forms.TextInput({
'class': 'form-control',
}))
orchidNumber = forms.CharField( required=False,
widget=forms.TextInput({
'class': 'form-control',
}))
institution = forms.ChoiceField( choices = [("Inst1","Inst1"), ("Inst2","Inst2"),("Other","Other")]
,widget=forms.Select({
'class': 'form-control',
}))
otherInstitute = forms.CharField( required=False,
widget=forms.TextInput({
'class': 'form-control',
}))
cstaPI = forms.CharField(
widget=forms.TextInput({
'class': 'form-control',
}))
class Meta:
model = UserInformation
exclude = ()
views.py
@csrf_protect
def register(request):
if request.method == 'POST':
form = UserInformationForm(request.POST)
form.save()
return HttpResponseRedirect('/register/success/')
else:
form = UserInformationForm()
variables = { 'form': form }
return render(request, 'registration/register.html',variables)
答案 0 :(得分:1)
您尚未在任何地方拨打is_valid
。它应该是:
if request.method == 'POST':
form = UserInformationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/register/success/')