I'm trying to create a form. Here is my form class:
class RegisterForm(forms.Form):
login=forms.CharField(min_length=5,max_length=15)
password=forms.CharField(min_length=5,max_length=15,widget=forms.PasswordInput)
passwordConfirmation=forms.CharField(min_length=5,max_length=15,label="Re enter password",widget=forms.PasswordInput)
email=forms.EmailField(min_length=5,max_length=20)
question=forms.CharField(min_length=8,max_length=20,label="Security question")
answer=forms.CharField(min_length=5开发者_JAVA百科,max_length=20,widget=forms.PasswordInput)
answerConfirmation=forms.CharField(min_length=5,max_length=20,label="Re enter answer",widget=forms.PasswordInput)'
And now i have tamplate as follow:
{% if form.login.errors %}
{{ form.login.errors }}
{% endif %}
{{ form.login }}<label for="login">Enter desired login</label><br />
And so on i just only change form.name etc. to one from the forms class. And when i filled form incorrect i don't get any error or nothing just blank form. Where I made a mistake? Thx for help Edit: Sorry i forget to show my function here is it
def register(request):
if request.method == 'POST':
form=RegisterForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/register')
else:
form = RegisterForm(auto_id=False)
return render_to_response('misc/register.html',locals(),context_instance=RequestContext(request))
else:
form=RegisterForm(auto_id=False)
return render_to_response('misc/register.html',locals(),context_instance=RequestContext(request))
form = RegisterForm(auto_id=False)
On this line you are creating new blank form and all validation errors are lost. Comment it out.
You've redeclared the form in the first else clause, so you've overwritten the errors. Drop that else clause completely, bring the very last line back one indent, so it catches the case when the firm is not valid.
精彩评论