How can I print form errors from inside the test

76 views
Skip to first unread message

Mario Gudelj

unread,
Mar 13, 2012, 10:06:13 PM3/13/12
to django...@googlegroups.com
Hi djangoers,

I have a problem where I'm running some form tests, the form validation is failing, and I'm not sure why it's failing and I can't figure out how to print the errors.

I preload the data into the form and when I validate it returns False.

>>> form.is_valid()
False

If I print the form and look at the HTML code everything looks fine as it would on the front end

>>> print form

But if I try

>>> print form.errors

I get nothing.

I also tried all individual files with

>>> print form['is_recur'].errors

and I still get nothing. 

There are no custom validation functions in this form, so I really don't knoyw why it wouldn't validate. 

Any help will be greatly appreciated.

Thanks!

-m

Daniel Roseman

unread,
Mar 14, 2012, 1:48:04 AM3/14/12
to django...@googlegroups.com
On Tuesday, 13 March 2012 19:06:13 UTC-7, somecallitblues wrote:
I preload the data into the form and when I validate it returns False.


*How* do you "preload" the data? How do you validate it?
--
DR. 

Karen Tracey

unread,
Mar 14, 2012, 8:20:19 AM3/14/12
to django...@googlegroups.com
The most common reason I have seen for is_valid() to return False but
errors to be empty is for the form to be unbound. Unbound forms are
never valid, but they also don't have any errors. Which goes to
Daniel's question: how exactly have you "preloaded" the form with
data?

Karen
--
http://tracey.org/kmt/

Mario Gudelj

unread,
Mar 14, 2012, 7:43:12 PM3/14/12
to django...@googlegroups.com
Thanks Daniel and Karen,

Karen, I;'m not entirely sure what an unbound form is. :)

Anyway, this is my test code:

def test_appointment_form(self):

        c = Client()

        base_data = {
            'name':'Foo name',
            'slug':'foo-session',
            'short_descr':'foo tagline',
            'long_descr':'long foo description',
            'staff':self.staff,
            'business':self.business,
            'start_date':'2012-07-24',
            'end_date':'2012-07-24',
        }
        self.form = AppointmentForm(business=self.business,staff=self.staff,initial=base_data)

        self.failUnless(self.form.is_valid()) #<---- This fails

        print self.form.errors #<----This doesn't print anything

        response = c.post('/console/appointments/add', {'form':self.form})
        self.assertEqual(response.status_code, 302)

This is form code:

class AppointmentForm(forms.Form):
    name = forms.CharField(label='Appointment Name (80 Characters Max)', max_length=NAME)
    short_descr = forms.CharField(label='Tagline', max_length=SUMMARY)
    #some more stuff here and the __init__ function below it

I'm not going the right way about this, am I? :) How can I see why the validation fails in my unit test?

I get the same thing if I work in shell.

Cheers,

-m



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


Daniel Roseman

unread,
Mar 14, 2012, 9:20:37 PM3/14/12
to django...@googlegroups.com
On Wednesday, 14 March 2012 16:43:12 UTC-7, somecallitblues wrote:
Thanks Daniel and Karen,

Karen, I;'m not entirely sure what an unbound form is. :)

Then you must not have read the documentation[1], which goes into great detail about bound and unbound forms. That's pretty much a prerequisite before asking for help, I'm afraid.
 

Anyway, this is my test code:

def test_appointment_form(self):

        c = Client()

        base_data = {
            'name':'Foo name',
            'slug':'foo-session',
            'short_descr':'foo tagline',
            'long_descr':'long foo description',
            'staff':self.staff,
            'business':self.business,
            'start_date':'2012-07-24',
            'end_date':'2012-07-24',
        }
        self.form = AppointmentForm(business=self.business,staff=self.staff,initial=base_data)

As that documentation points out, `initial` does not bind the form.
 
        self.failUnless(self.form.is_valid()) #<---- This fails

        print self.form.errors #<----This doesn't print anything

        response = c.post('/console/appointments/add', {'form':self.form})

Not the cause of your current problem, but this makes no sense: you can't POST a form object.


--
DR.


Mario Gudelj

unread,
Mar 15, 2012, 7:12:58 AM3/15/12
to django...@googlegroups.com
All I had to do is to change initial=base_data to data=base_data and the thing worked. is_valid() and is_bound now work.

Cheers,

-m

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/r8lcpGOgA5gJ.
Reply all
Reply to author
Forward
0 new messages