On Thursday, 11 July 2013 23:08:25 UTC+1, Kakar wrote:

> Hi,
> I am learning Django from an old Django version book, and i am stuck with 
> a problem regarding forms. When i render forms, i get the registration 
> right, but if its incorrect, it does not show the error msg in the html. I 
> tried {{form.errors}}, but couldn't fix he problem. Please guide me.
> Thank you.
>
 

> <snip>
>
> def register_page(request):
>     if request.method == 'POST':
>         form = RegistrationForm(request.POST)
>         if form.is_valid():
>             user = User.objects.create_user(
>                 username=form.cleaned_data['username'],
>                 email=form.cleaned_data['email'],
>                 password=form.cleaned_data['password1']
>                 )
>             return HttpResponseRedirect('/register/success/')
>         else:
>             form = RegistrationForm()
>             variables = RequestContext(request,{
>             'form':form
>             })
>             return 
> render_to_response('registration/register.html',variables)
>     else:
>         form = RegistrationForm()
>         variables = RequestContext(request,{
>             'form':form
>             })
>         return render_to_response('registration/register.html',variables)
>
> <snip>
>

You are re-instantiating the form in the first else clause, if it is not 
valid. That new form instance is empty and therefore doesn't have any 
errors.

You don't actually need that else clause at all: Just move the last two 
lines of the function ('variables' and 'return') back one indent level, and 
execution will fall through to that point if the form is invalid, complete 
with errors.
--
DR. 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to