2016-01-07 135 views
0

我有Django窗體,但它們不會變紅或在空時顯示錯誤(它們是必需的)。我有點擔心該怎麼做。Django窗體不顯示錯誤時空

views.py

def lOut(request): 
    if request.user.is_authenticated(): 
     context = { 
      "extendVar":"baseLoggedIn.html", 
     } 
    if request.user.is_authenticated()==False: 
     form=registerForm(request.POST or None) 
     if form.is_valid(): 
      email = form.cleaned_data['email'] 
      print ("yup") 
     elif form.is_valid()==False: 
      print ("nup") 
      print (form.errors) 
     print("eh")  
     context= { 
      "extendVar":"baseNotLoggedIn.html", 
      "form":form, 
     } 

base.html文件:

{% extends extendVar %} 

{% block title %} 
    {{ block.super }} lagro 
{% endblock %} 

{% block fname %} 
    {{ form.fname }} 
{% endblock %} 

{% block lname %} 
    {{ form.lname }} 
{% endblock %} 

{% block email %} 
    {{ form.email }} 
{% endblock %} 

{% block emailConfirm %} 
    {{ form.emailConfirm }} 
{% endblock %} 

{% block password %} 
    {{ form.password }} 
{% endblock %} 

baseNotLoggedIn.html

<form method="post" action="{% url 'try3' %}"> 
    {% csrf_token %} 
    <div class="row row-buffer"> 
     <div class="col-sm-6" style="padding-right:5px;" > 
     {% block fname %} {% endblock %}     
     <!-- <input class="form-control" placeholder="First Name" id="fname"> --> 
     </div> 

     <div class="col-sm-6" style="padding-left:5px;"> 
     {% block lname %} {% endblock %} 
     <!-- <input class="form-control" placeholder="Last Name" id="lname"> --> 
     </div> 

    </div> 
    <div class="row row-buffer"> 
     <div class="col-sm-12 form-group-lg"> 
     {% block email %} {% endblock %} 
     <!-- <input class="form-control" placeholder="Email" id="email"> --> 

     </div> 
    </div> 

    <div class="row row-buffer"> 
     <div class="col-sm-12 form-group-lg"> 
     {% block emailConfirm %} {% endblock %} 
     </div> 
    </div> 

    <div class="row row-buffer"> 
     <div class="col-sm-12 form-group-lg"> 
     {% block password %} {% endblock %}     
     <!-- <input type="password" class="form-control" placeholder="Password" id="password"> --> 
     </div> 
    </div> 

    <div class="row row-buffer"> 
     <div class="col-sm-12 form-group-lg"> 
     <button type = "submit" class="btn btn-success btn-lg" style="outline-width:0;">Submit</button> 
     </div> 
    </div> 
    </form> 

forms.py

class registerForm(forms.Form): 
    fname = forms.CharField(max_length=256, label="", required=True, widget=forms.TextInput(attrs={'class': 'form-control','placeholder': 'First Name',})) 
    lname = forms.CharField(max_length=256, label="", required=True, widget=forms.TextInput(attrs={'class': 'form-control','placeholder': 'Last Name',})) 
    email = forms.EmailField(max_length=256, label="", required=True, widget=forms.EmailInput(attrs={'class': 'form-control','placeholder': 'Email',})) 
    emailConfirm = forms.EmailField(max_length=256, label="", required=True, widget=forms.EmailInput(attrs={'class': 'form-control','placeholder': 'Confirm email',})) 
    password = forms.CharField(max_length=256, label="", required=True, widget=forms.PasswordInput(attrs={'class': 'form-control','placeholder': 'Password',})) 

回答

0

它沒有顯示,因爲您沒有在模板中顯示任何錯誤。從django doc報價:

你需要確保我們照顧每個字段的任何錯誤 任何錯誤的形式作爲一個整體。請注意窗體頂部的{{form.non_field_errors}} 以及每個 字段上的錯誤的模板查找。使用{{form.name_of_field.errors}}顯示錶格 錯誤的列表。

閱讀django doc瞭解如何在模板中渲染表單錯誤。

0

將您的forms.py更改爲此。

class registerForm(forms.Form): 
    fname = forms.CharField(max_length=256, label="", required=True, widget=forms.TextInput(attrs={'class': 'form-control','placeholder': 'First Name','required': True})) 
    lname = forms.CharField(max_length=256, label="", required=True, widget=forms.TextInput(attrs={'class': 'form-control','placeholder': 'Last Name','required': True})) 
    email = forms.EmailField(max_length=256, label="", required=True, widget=forms.EmailInput(attrs={'class': 'form-control','placeholder': 'Email','required': True})) 
    emailConfirm = forms.EmailField(max_length=256, label="", required=True, widget=forms.EmailInput(attrs={'class': 'form-control','placeholder': 'Confirm email','required': True})) 
    password = forms.CharField(max_length=256, label="", required=True, widget=forms.PasswordInput(attrs={'class': 'form-control','placeholder': 'Password','required': True})) 

P.S.添加「必需的」:與您的attrs字典相同。

看到這個。

>>> name = forms.TextInput(attrs={'required': True}) 
>>> name.render('name', 'A name') 
'<input name="name" type="text" value="A name" required />' 

來源:https://docs.djangoproject.com/es/1.9/ref/forms/widgets/

+0

哦確定非常感謝!現在它工作。只是一個簡單的問題:對於表單,你是否使用了第三方,比如註冊或者只是製作自己的表單? – swedishfished

+0

我更喜歡我自己的形式。 – kartikmaji