2017-02-09 47 views
0

我有一个简单的注册表单。人们可以填写信息并提交表格时,数据应该存入数据库。必填字段验证程序在azure上的Django中不起作用

该应用程序在我的本地主机上正常工作。我的表单中有一些必填字段。当我在本地主机上运行应用程序时,我尝试提交没有填写必填字段的表单,然后显示一个弹出窗口,并说**该字段不能为空**,但是当我将其部署到我的天蓝色门户时,试图挽救这个没有填写必填字段,然后它会产生错误:

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) 

回答

1

您还没有在任何地方拨打is_valid。它应该是:

if request.method == 'POST': 
    form = UserInformationForm(request.POST) 
    if form.is_valid(): 
     form.save() 
     return HttpResponseRedirect('/register/success/') 
+0

我做到了前面,但随后抛出错误:局部变量“变量”引用之前线返回分配渲染(要求,“注册/ register.html”,变量) –

+0

您需要将'variables'的定义移回一个缩进。 –

+0

好的,它的工作很好谢谢,但如何要求字段将工作,这是我在这篇文章中的实际问题 –