Ticket #17795: t17795.diff

File t17795.diff, 5.9 KB (added by Adrien Lemaire, 13 years ago)

Add kwargs to context in all generic base classes get() + test

  • django/views/generic/dates.py

    diff --git a/django/views/generic/dates.py b/django/views/generic/dates.py
    index 5f5f959..d0a073b 100644
    a b class BaseDateListView(MultipleObjectMixin, DateMixin, View):  
    171171    def get(self, request, *args, **kwargs):
    172172        self.date_list, self.object_list, extra_context = self.get_dated_items()
    173173        context = self.get_context_data(object_list=self.object_list,
    174                                         date_list=self.date_list)
     174                                        date_list=self.date_list, **kwargs)
    175175        context.update(extra_context)
    176176        return self.render_to_response(context)
    177177
  • django/views/generic/detail.py

    diff --git a/django/views/generic/detail.py b/django/views/generic/detail.py
    index b9278bb..fe8460a 100644
    a b class SingleObjectMixin(object):  
    9696class BaseDetailView(SingleObjectMixin, View):
    9797    def get(self, request, *args, **kwargs):
    9898        self.object = self.get_object()
    99         context = self.get_context_data(object=self.object)
     99        context = self.get_context_data(object=self.object, **kwargs)
    100100        return self.render_to_response(context)
    101101
    102102
  • django/views/generic/edit.py

    diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
    index 3cade52..50f600d 100644
    a b class ProcessFormView(View):  
    129129    def get(self, request, *args, **kwargs):
    130130        form_class = self.get_form_class()
    131131        form = self.get_form(form_class)
    132         return self.render_to_response(self.get_context_data(form=form))
     132        kwargs['form'] = form
     133        return self.render_to_response(self.get_context_data(**kwargs))
    133134
    134135    def post(self, request, *args, **kwargs):
    135136        form_class = self.get_form_class()
  • django/views/generic/list.py

    diff --git a/django/views/generic/list.py b/django/views/generic/list.py
    index 9797356..63f2808 100644
    a b class BaseListView(MultipleObjectMixin, View):  
    116116        if not allow_empty and len(self.object_list) == 0:
    117117            raise Http404(_(u"Empty list and '%(class_name)s.allow_empty' is False.")
    118118                          % {'class_name': self.__class__.__name__})
    119         context = self.get_context_data(object_list=self.object_list)
     119        context = self.get_context_data(object_list=self.object_list, **kwargs)
    120120        return self.render_to_response(context)
    121121
    122122
  • tests/regressiontests/generic_views/edit.py

    diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
    index 182615a..d92bc99 100644
    a b class ModelFormMixinTests(TestCase):  
    1515        form_class = views.AuthorGetQuerySetFormView().get_form_class()
    1616        self.assertEqual(form_class._meta.model, Author)
    1717
     18
    1819class CreateViewTests(TestCase):
    1920    urls = 'regressiontests.generic_views.urls'
    2021
    class CreateViewTests(TestCase):  
    7879        self.assertRedirects(res, reverse('author_detail', kwargs={'pk': obj.pk}))
    7980        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
    8081
     82    def test_regression_t17795(self):
     83        res = self.client.get('/edit/authors/create/special/t17795/')
     84        self.assertEqual(res.status_code, 200)
     85        self.assertIn('key', res.context, msg='key from get_context_data is expected')
     86        self.assertIn('new_key', res.context, msg='new_key from get is expected')
     87
    8188    def test_create_without_redirect(self):
    8289        try:
    8390            res = self.client.post('/edit/authors/create/naive/',
    class CreateViewTests(TestCase):  
    9299        self.assertEqual(res.status_code, 302)
    93100        self.assertRedirects(res, 'http://testserver/accounts/login/?next=/edit/authors/create/restricted/')
    94101
     102
    95103class UpdateViewTests(TestCase):
    96104    urls = 'regressiontests.generic_views.urls'
    97105
    class UpdateViewTests(TestCase):  
    224232        self.assertRedirects(res, 'http://testserver/list/authors/')
    225233        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
    226234
     235
    227236class DeleteViewTests(TestCase):
    228237    urls = 'regressiontests.generic_views.urls'
    229238
  • tests/regressiontests/generic_views/urls.py

    diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py
    index 090ec73..355f568 100644
    a b urlpatterns = patterns('',  
    7171        views.AuthorCreate.as_view()),
    7272    (r'^edit/authors/create/special/$',
    7373        views.SpecializedAuthorCreate.as_view()),
     74    (r'^edit/authors/create/special/t17795/$',
     75        views.CreateView_t17795.as_view()),
    7476
    7577    (r'^edit/author/(?P<pk>\d+)/update/naive/$',
    7678        views.NaiveAuthorUpdate.as_view()),
  • tests/regressiontests/generic_views/views.py

    diff --git a/tests/regressiontests/generic_views/views.py b/tests/regressiontests/generic_views/views.py
    index 5ff9cf0..5284d57 100644
    a b class BookDetailGetObjectCustomQueryset(BookDetail):  
    184184    def get_object(self, queryset=None):
    185185        return super(BookDetailGetObjectCustomQueryset,self).get_object(
    186186            queryset=Book.objects.filter(pk=2))
     187
     188
     189class CreateView_t17795(generic.CreateView):
     190    model = Author
     191    form_class = AuthorForm
     192    template_name = 'generic_views/form.html'
     193    context_object_name = 'thingy'
     194
     195    def get_success_url(self):
     196        return reverse('author_detail', args=[self.object.id,])
     197
     198    def get_context_data(self, **kwargs):
     199        kwargs.update({'key': 'value'})
     200        return super(CreateView_t17795, self).get_context_data(**kwargs)
     201
     202    def get(self, request, *args, **kwargs):
     203        kwargs.update({'new_key': 'new value'})
     204        return super(CreateView_t17795, self).get(request, *args, **kwargs)
Back to Top