Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #17148 -- Fixed the signature of `WizardView.get_context_data()…

…` to play nicer with mixin inheritance. Thanks, Bradley Ayers and Stephan Jaekel.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17231 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3e790ae63266bee48ad659ddedc792219ee33a0e 1 parent 5a48cb5
Julien Phalip jphalip authored
1  django/contrib/formtools/tests/wizard/__init__.py
View
@@ -14,4 +14,5 @@
SessionWizardTests,
CookieWizardTests,
WizardTestKwargs,
+ WizardTestGenericViewInterface,
)
51 django/contrib/formtools/tests/wizard/wizardtests/tests.py
View
@@ -1,9 +1,12 @@
from __future__ import with_statement
import os
+from django import forms
from django.test import TestCase
+from django.test.client import RequestFactory
from django.conf import settings
from django.contrib.auth.models import User
+from django.contrib.formtools.wizard.views import CookieWizardView
class WizardTests(object):
@@ -280,3 +283,51 @@ def test_template(self):
TEMPLATE_DIRS=list(settings.TEMPLATE_DIRS) + [templates]):
response = self.client.get(self.wizard_url)
self.assertTemplateUsed(response, 'other_wizard_form.html')
+
+
+class WizardTestGenericViewInterface(TestCase):
+ def test_get_context_data_inheritance(self):
+ class TestWizard(CookieWizardView):
+ """
+ A subclass that implements ``get_context_data`` using the standard
+ protocol for generic views (accept only **kwargs).
+
+ See ticket #17148.
+ """
+ def get_context_data(self, **kwargs):
+ context = super(TestWizard, self).get_context_data(**kwargs)
+ context['test_key'] = 'test_value'
+ return context
+
+ factory = RequestFactory()
+ view = TestWizard.as_view([forms.Form])
+
+ response = view(factory.get('/'))
+ self.assertEquals(response.context_data['test_key'], 'test_value')
+
+ def test_get_context_data_with_mixin(self):
+ class AnotherMixin(object):
+ def get_context_data(self, **kwargs):
+ context = super(AnotherMixin, self).get_context_data(**kwargs)
+ context['another_key'] = 'another_value'
+ return context
+
+ class TestWizard(AnotherMixin, CookieWizardView):
+ """
+ A subclass that implements ``get_context_data`` using the standard
+ protocol for generic views (accept only **kwargs).
+
+ See ticket #17148.
+ """
+ def get_context_data(self, **kwargs):
+ context = super(TestWizard, self).get_context_data(**kwargs)
+ context['test_key'] = 'test_value'
+ return context
+
+ factory = RequestFactory()
+
+ view = TestWizard.as_view([forms.Form])
+
+ response = view(factory.get('/'))
+ self.assertEquals(response.context_data['test_key'], 'test_value')
+ self.assertEquals(response.context_data['another_key'], 'another_value')
9 django/contrib/formtools/wizard/views.py
View
@@ -497,7 +497,7 @@ def get_step_index(self, step=None):
step = self.steps.current
return self.get_form_list().keyOrder.index(step)
- def get_context_data(self, form, *args, **kwargs):
+ def get_context_data(self, form, **kwargs):
"""
Returns the template context for a step. You can overwrite this method
to add more data for all or some steps. This method returns a
@@ -514,12 +514,12 @@ def get_context_data(self, form, *args, **kwargs):
class MyWizard(FormWizard):
def get_context_data(self, form, **kwargs):
- context = super(MyWizard, self).get_context_data(form, **kwargs)
+ context = super(MyWizard, self).get_context_data(form=form, **kwargs)
if self.steps.current == 'my_step_name':
context.update({'another_var': True})
return context
"""
- context = super(WizardView, self).get_context_data(*args, **kwargs)
+ context = super(WizardView, self).get_context_data(**kwargs)
context.update(self.storage.extra_data)
context['wizard'] = {
'form': form,
@@ -535,7 +535,7 @@ def render(self, form=None, **kwargs):
Returns a ``HttpResponse`` containing all needed context data.
"""
form = form or self.get_form()
- context = self.get_context_data(form, **kwargs)
+ context = self.get_context_data(form=form, **kwargs)
return self.render_to_response(context)
def done(self, form_list, **kwargs):
@@ -683,4 +683,3 @@ class NamedUrlCookieWizardView(NamedUrlWizardView):
A NamedUrlFormWizard with pre-configured CookieStorageBackend.
"""
storage_name = 'django.contrib.formtools.wizard.storage.cookie.CookieStorage'
-
4 docs/ref/contrib/formtools/form-wizard.txt
View
@@ -309,7 +309,7 @@ Advanced ``WizardView`` methods
Example to add extra variables for a specific step::
def get_context_data(self, form, **kwargs):
- context = super(MyWizard, self).get_context_data(form, **kwargs)
+ context = super(MyWizard, self).get_context_data(form=form, **kwargs)
if self.steps.current == 'my_step_name':
context.update({'another_var': True})
return context
@@ -436,7 +436,7 @@ Advanced ``WizardView`` methods
def render(self, form=None, **kwargs):
form = form or self.get_form()
- context = self.get_context_data(form, **kwargs)
+ context = self.get_context_data(form=form, **kwargs)
return self.render_to_response(context)
Providing initial data for the forms
Please sign in to comment.
Something went wrong with that request. Please try again.