Friday, July 30, 2010

Re: Form Validation

On Jul 30, 4:49 pm, rupert <evan.fer...@gmail.com> wrote:
> Here's the view template:
>
> def respondant(request):
>
>         user = request.user
>
>         if set(RESPONDANT_FIELDS).issubset(set([key for key,value in
> request.POST.items()])):
>                 form = RespondantForm(request.POST)
>                 if form.is_valid():
>                         form.save()
>                         template = "feedback/success.html"
>                 else:
>                         form = RespondantForm()
>                         return render_to_response('feedback/index.html', {
>                         'form': form,
>                 })
>
>         else:
>                 key = request.REQUEST.get('key')
>                 if key:
>                         respondant = Respondant.objects.check_key(key)
>                 else:
>                         form = RespondantForm()
>                         return render_to_response('feedback/index.html', {
>                         'form': form,
>                 })
>
>         return render_to_response(template)

This is an unusual structure for a form view, but the problem happens
in the first `else` clause. Previously you've instantiated the form,
passing it the POSTed values, and checked it for errors. But now you
re-instantiate a *blank* form, which obviously won't have any errors
attached to it. Instead of doing that, you should simply be passing
the existing form object into the template - in other words, you don't
need that `else` clause at all (I'm assuming that the extra
indentation of `return render_to_response` is just a cut-and-paste
error, and it's actually at the same level as the `if form.is_valid()`
line.)
--
DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment