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.