2017-02-09 28 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

好的,它的工作很好謝謝,但如何要求字段將工作,這是我在這篇文章中的實際問題 –