On Tue, Jul 13, 2010 at 4:06 PM, reduxdj <patricklemi...@gmail.com> wrote: > HI, > > Forgive me, here's another n00b doozie. I've created a form, which > extends form, however, my form is not validating and I can't get the > form to print errors next to my form fields. What's curious, at the > form.is_valid() > in all the examples, I don't see an else: clause. Which leads me to > believe that a validation error should be > raised at this point, so an else is not neccessary?? > > So, my form should hold clean_fieldname methods for each field name > correct? and if an error is raised, > automagically, django handles the error messages, as long as it's > defined in my template, correct or not? > So every property needs a clean method? > > Sorry, I've read the resources and I'm a little unclear on this? > > Thanks for your time, > Django and the community rocks > > from my views: > > def listing(request): > if request.method == 'POST': # If the form has been submitted... > new_listing = Listing() > form = ListingForm(data=request.POST,instance=new_listing) > #form.save() > if form.is_valid(): # All validation rules pass > form.save() > return HttpResponseRedirect('/listing/') # Redirect after > POST > #else: > #return HttpResponseRedirect('/listing/') # Redirect after > POST > #return HttpResponse('form is a failure, like you.') > #return HttpResponseRedirect('/create/') # Redirect after > POST > else: > form = ListingForm() # An unbound form > return render_to_response('listing.html', { > 'form': form, > }) >
Unindent the 'return render_to_response(...' lines, so that they are executed when you have an invalid form. The form will be re-rendered, except this time the form instance will be bound, and have appropriate error messages in it. eg: if request.method == 'POST': form = ... if form.is_valid(): ... else: form = ... return render_to_response(...) Cheers Tom -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.