我有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',}))
哦確定非常感謝!現在它工作。只是一個簡單的問題:對於表單,你是否使用了第三方,比如註冊或者只是製作自己的表單? – swedishfished
我更喜歡我自己的形式。 – kartikmaji