我有一個註冊頁面具有低於下面的代碼:的Django - 表單驗證未示出
在視圖:
def register_step2(request, value):
context={"type": value}
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
nric = form.cleaned_data['nric']
name = form.cleaned_data['name']
password = form.cleaned_data['password']
email = form.cleaned_data['email']
phonenumber = form.cleaned_data['phonenumber']
dob = form.cleaned_data['dob']
User.objects.create_user(nric, email, password)
return HttpResponseRedirect('/')
return render_to_response('register.html', context, RequestContext(request))
在register.html:
{% extends "base.html" %}
{% block subheader %}
{% if type == '0' %}
Are you..
{% endif %}
{% if type == '1' %}
Registration - Able Elder
{% endif %}
{% if type == '2' %}
Registration - Public
{% endif %}
{% endblock %}
{% block content %}
{% if type == '0' %}
<a href="/register/1">an elderly looking for something to do?</a>
<a href="/register/2">the public looking for someone to help you?</a>
{% else %}
<form action="/register/{{type}}/" method="post">{% csrf_token %}
<table class="center">
{% for field in able_form %}
<tr>
<td>
{{ field.label_tag }}:
</td>
<td>
{{ field.errors }}
{{ field }}
</td>
</tr>
{% endfor %}
<tr>
<td>
{{ able_form.non_field_errors }}
</td>
</tr>
<tr>
<td colspan="2">
By clicking on "register" I agree with the terms and conditions.
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="register">
</form>
</td>
{% endif %}
{% endblock %}
在context_processors .py(它在settings.py中正確設置)
from forms import RegisterForm
def able_form(request):
return {
'able_form' : RegisterForm()}
在forms.py
class RegisterForm(forms.Form):
nric = forms.CharField(max_length=NAME_MAX)
name = forms.CharField(max_length=NAME_MAX)
password = forms.CharField(widget=forms.PasswordInput)
confirm_password = forms.CharField(widget=forms.PasswordInput)
email = forms.EmailField()
postal_code = forms.IntegerField()
phone_number = forms.IntegerField()
date_of_birth = forms.DateField(widget=forms.DateInput)
這是我的理解,可變區域將顯示領域的錯誤,但是當我點擊註冊按鈕沒有顯示。我在這裏做錯了什麼?
我沒有看到一個窗體被傳遞到模板上下文..在哪裏'able_form'來自? –
表格可以正常顯示。我會更新我的問題 – causztic