On Apr 26, 11:57 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Tuesday, April 26, 2011 10:37:50 AM UTC+1, Derek wrote:
>
> > I have a form that I am expecting will redisplay with errors; it does not
> > do so.  Instead, the "print form.errors" statement is reached which does, in
> > fact, show that there are errors. For example:
>
> > <ul class="errorlist"><li>cellphone<ul class="errorlist"><li>This field is
> > required.</li></ul>
>
> > What else do I need to do to have the form be redisplayed?
>
> > Thanks
> > Derek
>
> > # form
> > class PersonForm(ModelForm):
> >     class Meta:
> >         model = Person
> >         fields = (
> >             'first_name', 'last_name', 'date_of_birth', 'gender', 'email',
> >             'cellphone', 'area',
> >             )
>
> > # view
> >     person = Person.objects.get(pk=id)
> >     if request.method == 'POST':
> >         form = PersonForm(request.POST, instance=person)
> >         if form.is_valid():
> >             form.save()
> >             return HttpResponseRedirect('/profile/')
> >         else:
> >             print form.errors
> >     else:
> >         form = PersonForm(instance=person)
> >         return render_to_response(
> >             'profile_edit.html',
> >             {'form': form},
> >             context_instance=RequestContext(request))
>
> Just move the final return call back one indent. The form will then be
> displayed in all cases other than successful validation.

Thanks Daniel for the hint.  For the record, this was the final code
that worked:

    # Get person to be edited
    person = Person.objects.get(pk=id)
    # Create form
    if request.method == 'POST':
        form = PersonForm(request.POST, instance=person)
        if form.is_valid():
            # Save form data if valid
            form.save()
            return HttpResponseRedirect('/profile/')
    else:
        form = PersonForm(instance=person)
    # Display form (also show any form errors)
    return render_to_response(
        'profile_edit.html',
        {
            'username': request.user.username,
            'profile_info': user_profile,
            'form': form
        },
        context_instance=RequestContext(request))

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to