Template not displaying validation errors

5,681 views
Skip to first unread message

mhulse

unread,
May 5, 2010, 6:04:40 PM5/5/10
to Django users
Hi,

I have spent half the day googling and testing my code with no luck
getting an error message to appear via my template. I am hoping ya'll
could provide a little assistance. :)

forms.py:

============

class SearchForm(forms.Form):
q = forms.CharField(
max_length = 128,
min_length = 2,
label = 'Keywords',
help_text = 'This is a required field'
)
def clean_q(self):
cd = self.cleaned_data['q']
if not cd:
raise forms.ValidationError("Please enter a search term.")
return cd
# More fields defined here ....

============

views.py:

============

def search(request):

pg = request.GET.get('page', '1')
results = ''
form = SearchForm()

if request.method == 'GET': # If the form has been submitted...

form = SearchForm(request.GET) # A form bound to the GET data.

if form.is_valid(): # All validation rules pass.

# Q objects/pagination here...

else:

form = SearchForm()

return render_to_response(
'folder/search.html',
{
'form': form,
'results': results,
}
)

============

search.html (nothing is output in any of the below tags):

============

{{ form.non_field_errors }}
{% if form.errors %}
{{ form.errors|pluralize }}
{% endif %}
{% for field in form %}
{{ field.error }}
{{ field.field.error }}
{% endfor %}

============

I must be missing something really obvious here! :D

Almost all of the examples of django forms and validation use POST
data... Is there something about GET data that makes error handling
different?

TIA!

Cheers,
Micky

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

Karen Tracey

unread,
May 5, 2010, 6:11:50 PM5/5/10
to django...@googlegroups.com
On Wed, May 5, 2010 at 6:04 PM, mhulse <rgm...@gmail.com> wrote:
               if form.is_valid(): # All validation rules pass.

                       # Q objects/pagination here...

               else:

                       form = SearchForm()


form.is_valid() is what annotates the existing form with error information in the case where there are errrors. However, if form.is_valid() returns False, the code here immediately overwrites the existing (now annotated with error information) form and replaces it with a newly-created blank form, so there will be no errors for the template to display. Get rid of that else leg.

Karen

mhulse

unread,
May 5, 2010, 6:20:03 PM5/5/10
to Django users
Hi Karen! Thanks so much for your quick reply, I really appreciate
it. :)

> form.is_valid() is what annotates the existing form with error information
> in the case where there are errrors. However, if form.is_valid() returns
> False, the code here immediately overwrites the existing (now annotated with
> error information) form and replaces it with a newly-created blank form, so
> there will be no errors for the template to display. Get rid of that else
> leg.

Eureka! That did the trick!!!

I swear that every example I looked at today returned an unbound
form. :D

Onward!

Thanks again Karen. You rock!

Cheers,
Micky
Reply all
Reply to author
Forward
0 new messages