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',
{'filter_form':filter_form,

#'current_ratings_list':current_ratings_list,
#'temp':temp},
},

context_instance=RequestContext(request))
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

form >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
class RatingsListFilterForm(forms.Form):
organisation_name = forms.CharField(required=False,
widget=forms.TextInput(attrs={'class':'textbox'}))
corporation = forms.ModelChoiceField(required=False,
queryset=Corporation.objects.all(),
widget=forms.Select(attrs={'class':'selector'}))
....
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

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

Regards

ALJ

On Mar 30, 2:10 am, Karen Tracey <kmtra...@gmail.com> wrote:
> On Tue, Mar 29, 2011 at 4:40 PM, ALJ <astley.lejas...@gmail.com> 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:
>
> >http://127.0.0.1:8001/myapp/?organisation_name=&corporation=4&authori...
>
> > 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
> --http://tracey.org/kmt/

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

No comments:

Post a Comment