我有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 :(得分:0)
它没有显示,因为您没有在模板中呈现任何错误。引自django doc:
您需要确保我们处理每个字段的任何错误 整个表格的任何错误。注意{{form.non_field_errors}} 在表单的顶部和模板查找每个错误 领域。使用{{form.name_of_field.errors}}显示表单列表 错误。
阅读django doc,了解如何在模板中呈现表单错误。
答案 1 :(得分: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。添加&#39;必需&#39;:对你的attrs dict来说是真的。
看到这一点。
>>> name = forms.TextInput(attrs={'required': True})
>>> name.render('name', 'A name')
'<input name="name" type="text" value="A name" required />'