Wednesday, March 30, 2011

Re: Invalid values in a form cause validation to fail

Hi Karen,

Here is an example that I use to replicate the error:

view >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def myview(request)
filter_form = RatingsListFilterForm(request.GET or None)
if filter_form.is_valid():
return HttpResponse("Worked!")

return render_to_response('ratings_list.html',



form >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
class RatingsListFilterForm(forms.Form):
organisation_name = forms.CharField(required=False,
corporation = forms.ModelChoiceField(required=False,

File "C:\Python26\lib\site-packages\django\core\handlers\" in
92. response = callback(request, *callback_args,
File "C:\Python26\lib\site-packages\django\contrib\auth\"
in __call__
78. return self.view_func(request, *args, **kwargs)
File "E:\Programming\mysite\svs\" in _function
25. return function(request,*args, **kwargs)
File "E:\Programming\mysite\svs\" in ratings_list
36. if filter_form.is_valid():
File "C:\Python26\lib\site-packages\django\forms\" in is_valid
120. return self.is_bound and not bool(self.errors)
File "C:\Python26\lib\site-packages\django\forms\" in
111. self.full_clean()
File "C:\Python26\lib\site-packages\django\forms\" in
240. value = field.clean(value)
File "C:\Python26\lib\site-packages\django\forms\" in clean
993. value = self.queryset.get(**{key: value})
File "C:\Python26\lib\site-packages\django\db\models\" in get
299. clone = self.filter(*args, **kwargs)
File "C:\Python26\lib\site-packages\django\db\models\" in
498. return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Python26\lib\site-packages\django\db\models\" in
516. clone.query.add_q(Q(*args, **kwargs))
File "C:\Python26\lib\site-packages\django\db\models\sql\" in
1675. can_reuse=used_aliases)
File "C:\Python26\lib\site-packages\django\db\models\sql\" in
1614. connector)
File "C:\Python26\lib\site-packages\django\db\models\sql\" in
56. obj, params = obj.process(lookup_type, value)
File "C:\Python26\lib\site-packages\django\db\models\sql\" in
269. params =
self.field.get_db_prep_lookup(lookup_type, value)
File "C:\Python26\lib\site-packages\django\db\models\fields
\" in get_db_prep_lookup
210. return [self.get_db_prep_value(value)]
File "C:\Python26\lib\site-packages\django\db\models\fields
\" in get_db_prep_value
361. return int(value)

Exception Type: ValueError at /svs/
Exception Value: invalid literal for int() with base 10: 'ww'

I'm using Django 1.1 if that makes any difference



On Mar 30, 2:10 am, Karen Tracey <> wrote:
> On Tue, Mar 29, 2011 at 4:40 PM, ALJ <> wrote:
> > I have a form within a view which allows users to filter results
> > within a database. The request is a GET because I want the users to be
> > able to bookmark the results.
> > My querystring looks something like:
> >
> > My view checks the form as it comes in (form.is_valid()) and bounces
> > them back if they have been messing about with the querystring. The
> > validation picks up errors if I do something like
> > "..&corporation=999999&...", but if I do something like
> > "...&corporation=wwww&...." it brings up a traceback
> > Exception Value:
> > invalid literal for int() with base 10: 'wwww'
> > I would have expected that the validation process would have picked up
> > that it isn't valid and just returned field error.
> It does, in general. This form/view:
> class TestForm(forms.Form):
>     num = forms.IntegerField()
> def testme(request):
>     if request.GET:
>         tf = TestForm(request.GET)
>         if tf.is_valid():
>             return http.HttpResponse("Worked!")
>     else:
>         tf = TestForm()
>     return render_to_response('form.html', {'form': tf})
> rendered with a method="get" form, causes re-display of the form with an
> error message "Enter a whole number." associated with the num field if the
> value www is entered in the num field (or simply retrieved by a ?num=www
> querystring).
> How is what you are doing different from what is shown above? You have not
> shown any of your code nor the full traceback, so it is hard to guess what
> may be the cause of the behavior you are seeing.
> Karen
> --

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment