Background Image
Table of Contents Table of Contents
Previous Page  444 / 478 Next Page
Information
Show Menu
Previous Page 444 / 478 Next Page
Page Background

'get_template_names()'

ERROR: test_invalid_list_items_arent_saved (lists.tests.test_views.NewListTest)

django.core.exceptions.ImproperlyConfigured: TemplateResponseMixin requires

either a definition of 'template_name' or an implementation of

'get_template_names()'

ERROR: test_redirects_after_POST (lists.tests.test_views.NewListTest)

TypeError: save() missing 1 required positional argument: 'for_list'

ERROR: test_saving_a_POST_request (lists.tests.test_views.NewListTest)

TypeError: save() missing 1 required positional argument: 'for_list'

ERROR: test_validation_errors_are_shown_on_home_page (lists.tests.test_views.NewListTest)

django.core.exceptions.ImproperlyConfigured: TemplateResponseMixin requires

either a definition of 'template_name' or an implementation of

'get_template_names()'

Ran 34 tests in 0.125s

FAILED (errors=6)

Let’s start with the third—maybe we can just add the template?

lists/views.py (ch31l005).

class

NewListView

(

CreateView

):

form_class

=

ItemForm

template_name

=

'home.html'

That gets us down to just two failures: we can see they’re both happening in the generic

view’s

form_valid

function, and that’s one of the ones that you can override to provide

custom behaviour in a CBGV. As its name implies, it’s run when the view has detected

a valid form. We can just copy some of the code from our old view function, that used

to live after

if form.is_valid():

:

lists/views.py (ch31l005).

class

NewListView

(

CreateView

):

template_name

=

'home.html'

form_class

=

ItemForm

def

form_valid

(

self

,

form

):

list_

=

List

.

objects

.

create

()

form

.

save

(

for_list

=

list_

)

return

redirect

(

list_

)

That gets us a full pass!

$

python3 manage.py test lists

Ran 34 tests in 0.119s

OK

$

python3 manage.py test functional_tests

Ran 4 tests in 15.157s

OK

And we

could

even save two more lines, trying to obey “DRY”, by using one of the main

advantages of CBVs: inheritance!

416

|

Appendix B: Django Class-Based Views