4

I have the following code

---- urls.py ----

url(r'^(?P<city_slug>[-\w]+)/$',
    BookingWizard.as_view(),
    name='city_booking'),

---- views.py ----

class BookingWizard(SessionWizardView):

    def get_context_data(self, form, **kwargs):
         context = super(BookingWizard, self).get_context_data(form, **kwargs)
         cities = City.objects.all()
         context.update({'cities': cities,
                         'city': City.objects.get(slug=kwargs['city_slug'])})
        return context

The problem is I'm getting key error trying to access kwargs['city_slug'] in the get_context_data() method.

although I can access kwargs['city_slug'] in the done() method with no problems.

Any ideas?

4

1 回答 1

6

您可以使用self.kwargs. 这是因为它被设置在.的超类的as_view()方法中。ViewSessionWizardView

https://github.com/django/django/blob/master/django/views/generic/base.py#L61-68

于 2013-04-29T11:52:52.013 回答