newbie question: .is_valid seems non-Boolean?

5 views
Skip to first unread message

BobAalsma

unread,
Dec 20, 2009, 8:17:30 AM12/20/09
to Django users
I'm following the The Django Book 2.0, trying to learn Django.
I'm on Apple OS X10.5.8, Python 2.5.1.
I have looked at FAQ, mailing lists, etc. but did not find any
situation looking like this.

Question:
although all other parts seem to work as described in the book,
the .is_valid returns an unexpected non Boolean answer - how to addres
this?

I'll copy some of my shell log to show you the problem (following a
part of chapter 8 of The Django Book):
>>> from beppe.forms import ContactForm
>>> f = ContactForm()
>>> f = ContactForm({'subject': 'Hello', 'email': 'A...@a.nl', 'message': 'nice site heb je hier'})
>>> f.is_valid
<bound method ContactForm.is_valid of <beppe.forms.ContactForm object
at 0x7ac990>>
>>> f = ContactForm({'subject': 'Hello', 'email': 'A...@a.nl'})
>>> f.is_valid
<bound method ContactForm.is_valid of <beppe.forms.ContactForm object
at 0x7ac970>>
>>> f.is_bound
True
>>> f.errors
{'message': [u'This field is required.']}
>>> f = ContactForm({'subject': 'Hello', 'email': 'A...@a.nl', 'message': 'nice site heb je hier'})
>>> f.errors
{}
>>> f.is_bound
True
>>> f.is_valid
<bound method ContactForm.is_valid of <beppe.forms.ContactForm object
at 0x7ac9f0>>

Karen Tracey

unread,
Dec 20, 2009, 8:24:29 AM12/20/09
to django...@googlegroups.com
On Sun, Dec 20, 2009 at 8:17 AM, BobAalsma <bob.a...@aalsmacons.nl> wrote:
I'm following the The Django Book 2.0, trying to learn Django.
I'm on Apple OS X10.5.8, Python 2.5.1.
I have looked at FAQ, mailing lists, etc. but did not find any
situation looking like this.

Question:
although all other parts seem to work as described in the book,
the .is_valid returns an unexpected non Boolean answer - how to addres
this?

is_valid is a method.  You need to call it in order to get its return value:

f.is_valid()

not:

f.is_valid

Karen

Łukasz Balcerzak

unread,
Dec 20, 2009, 8:26:21 AM12/20/09
to django...@googlegroups.com
Well, Form.is_valid calculates the form and returns boolean, thats for sure.
Specifically, "<bound method ContactForm.is_valid of <beppe.forms.ContactForm object"
means that "is_valid" is a ``method`` bounded to instance of your form class.

Look at this:

>>> from django import forms
>>>
>>> class F(forms.Form):
... name = forms.CharField()
...
>>> f = F()
>>> f.is_valid
<bound method F.is_valid of <__main__.F object at 0x1010cc090>>
>>> f.is_valid()
False
>>> f = F({'name': 'Lukasz'})
>>> f.is_valid()
True

You get your boolean if you ``call`` the "is_valid" method.

> --
>
> 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.
>
>

Reply all
Reply to author
Forward
0 new messages