views:

179

answers:

2

I love how Django performs form validation. However, I want to override the way the errors get displayed. I want to send form validation errors to the top of my web page (to a specific div tag) instead of letting Django display errors next to the form fields that are invalid. Any suggestions?

+1  A: 

You can easily do this by simply specifying {{ form.errors }} at the top of your template, rather than putting {{ field.errors }} at each field level.

I suspect from your question though that you're just using the {{ form.as_p }} tag, or one of its brethren, to output the whole form in one go. Don't do this if you want to customise the display at all. It's not very difficult to replace it with a simple for loop to go through each field and display its label_tag and the field itself, and you benefit hugely from the increased control.

Daniel Roseman
Daniel, thanks so much - that works. However, Django is still displaying its error messages too ie. I've got mine appearing at the top of my page, and Django is still displaying errors next to the fields that are invalid. Is it possible now to tell Django not to display the validation errors (but still generate form.errors for me to use)?
Peter there are two things :1) errors associated with fields {{field.erorrs}}2) global errors which are not associated with fields {{form.errors}}So once you validate remove all the field.errors and copy them into form.errors.Then all error messages will be displayed on the top.
Rama Vadakattu
A: 

Daniel pointed you in the right direction. See the documentation here:

http://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-form-template

"Each named form-field can be output to the template using {{ form.name-of-field }}, which will produce the HTML needed to display the form widget. Using {{ form.name-of-field.errors }} displays a list of field errors...."

Harold