Ticket #16744: 16744.diff

File 16744.diff, 8.8 KB (added by tobias, 4 years ago)

patch reflecting the requested doc and code changes in the pull request

  • django/views/generic/base.py

    diff --git a/django/views/generic/base.py b/django/views/generic/base.py
    index ea14281..45e0a99 100644
    a b class TemplateView(TemplateResponseMixin, View): 
    117117    """
    118118    def get_context_data(self, **kwargs):
    119119        return {
    120             'params': kwargs
     120            'params': kwargs,
     121            'view': self
    121122        }
    122123
    123124    def get(self, request, *args, **kwargs):
  • django/views/generic/detail.py

    diff --git a/django/views/generic/detail.py b/django/views/generic/detail.py
    index ab21573..6e84cb5 100644
    a b class SingleObjectMixin(object): 
    8787
    8888    def get_context_data(self, **kwargs):
    8989        context = kwargs
     90        if 'view' not in context:
     91            context['view'] = self
    9092        context_object_name = self.get_context_object_name(self.object)
    9193        if context_object_name:
    9294            context[context_object_name] = self.object
  • django/views/generic/edit.py

    diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
    index 3cade52..6c53e27 100644
    a b class FormMixin(object): 
    4646        return kwargs
    4747
    4848    def get_context_data(self, **kwargs):
     49        if 'view' not in kwargs:
     50            kwargs['view'] = self
    4951        return kwargs
    5052
    5153    def get_success_url(self):
    class ModelFormMixin(FormMixin, SingleObjectMixin): 
    9092
    9193    def get_form_kwargs(self):
    9294        """
    93         Returns the keyword arguments for instanciating the form.
     95        Returns the keyword arguments for instantiating the form.
    9496        """
    9597        kwargs = super(ModelFormMixin, self).get_form_kwargs()
    9698        kwargs.update({'instance': self.object})
    class ModelFormMixin(FormMixin, SingleObjectMixin): 
    114116
    115117    def get_context_data(self, **kwargs):
    116118        context = kwargs
     119        if 'view' not in context:
     120            context['view'] = self
    117121        if self.object:
    118122            context['object'] = self.object
    119123            context_object_name = self.get_context_object_name(self.object)
  • django/views/generic/list.py

    diff --git a/django/views/generic/list.py b/django/views/generic/list.py
    index 9797356..e04656a 100644
    a b class MultipleObjectMixin(object): 
    9494                'paginator': paginator,
    9595                'page_obj': page,
    9696                'is_paginated': is_paginated,
    97                 'object_list': queryset
     97                'object_list': queryset,
     98                'view': self
    9899            }
    99100        else:
    100101            context = {
    101102                'paginator': None,
    102103                'page_obj': None,
    103104                'is_paginated': False,
    104                 'object_list': queryset
     105                'object_list': queryset,
     106                'view': self
    105107            }
    106108        context.update(kwargs)
    107109        if context_object_name is not None:
  • docs/ref/class-based-views.txt

    diff --git a/docs/ref/class-based-views.txt b/docs/ref/class-based-views.txt
    index 692417e..a851bf8 100644
    a b View 
    879879        The default implementation returns ``HttpResponseNotAllowed`` with list
    880880        of allowed methods in plain text.
    881881
     882    **Context**
     883
     884    .. versionadded:: 1.4
     885
     886    * ``view``: The template context of all class-based generic views will
     887      include a ``view`` variable that points to the ``View`` instance.
     888
     889      .. admonition:: Use ``alters_data`` where appropriate
     890
     891          Note that having the view instance in the template context may expose
     892          potentially hazardous methods to template authors.  To prevent methods
     893          like this from being called in the template, set ``alters_data=True``
     894          on those methods.  For more information, the documention on
     895          :ref:`rendering a template context <start-alters-data-description>`.
     896
    882897TemplateView
    883898~~~~~~~~~~~~
    884899.. class:: TemplateView()
  • docs/ref/templates/api.txt

    diff --git a/docs/ref/templates/api.txt b/docs/ref/templates/api.txt
    index 2b7d354..6bae4ef 100644
    a b straight lookups. Here are some things to keep in mind: 
    195195* A variable can only be called if it has no required arguments. Otherwise,
    196196  the system will return an empty string.
    197197
     198.. _start-alters-data-description:
     199
    198200* Obviously, there can be side effects when calling some variables, and
    199201  it'd be either foolish or a security hole to allow the template system
    200202  to access them.
  • tests/regressiontests/generic_views/base.py

    diff --git a/tests/regressiontests/generic_views/base.py b/tests/regressiontests/generic_views/base.py
    index d9debb6..1e03577 100644
    a b class TemplateViewTest(TestCase): 
    209209        self.assertEqual(response.status_code, 200)
    210210        self.assertEqual(response.context['params'], {'foo': 'bar'})
    211211
     212    def test_view_in_template_context(self):
     213        """
     214        A generic template view includes the 'view' in the context.
     215        """
     216        response = self.client.get('/template/simple/bar/')
     217        self.assertEqual(response.status_code, 200)
     218        self.assertTrue(isinstance(response.context['view'], View))
     219
    212220    def test_extra_template_params(self):
    213221        """
    214222        A template view can be customized to return extra context.
  • tests/regressiontests/generic_views/detail.py

    diff --git a/tests/regressiontests/generic_views/detail.py b/tests/regressiontests/generic_views/detail.py
    index 0b5d873..10d0dbd 100644
    a b from __future__ import absolute_import 
    22
    33from django.core.exceptions import ImproperlyConfigured
    44from django.test import TestCase
     5from django.views.generic.base import View
    56
    67from .models import Artist, Author, Page
    78
    class DetailViewTest(TestCase): 
    1011    fixtures = ['generic-views-test-data.json']
    1112    urls = 'regressiontests.generic_views.urls'
    1213
     14    def test_view_in_template_context(self):
     15        res = self.client.get('/detail/obj/')
     16        self.assertTrue(isinstance(res.context['view'], View))
     17
    1318    def test_simple_object(self):
    1419        res = self.client.get('/detail/obj/')
    1520        self.assertEqual(res.status_code, 200)
  • tests/regressiontests/generic_views/edit.py

    diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
    index 182615a..060b41a 100644
    a b from django.core.urlresolvers import reverse 
    55from django import forms
    66from django.test import TestCase
    77from django.utils.unittest import expectedFailure
     8from django.views.generic.base import View
    89
    910from . import views
    1011from .models import Artist, Author
    class CreateViewTests(TestCase): 
    9293        self.assertEqual(res.status_code, 302)
    9394        self.assertRedirects(res, 'http://testserver/accounts/login/?next=/edit/authors/create/restricted/')
    9495
     96    def test_create_view_in_context(self):
     97        res = self.client.get('/edit/authors/create/')
     98        self.assertEqual(res.status_code, 200)
     99        self.assertTrue(isinstance(res.context['view'], View))
     100
     101
    95102class UpdateViewTests(TestCase):
    96103    urls = 'regressiontests.generic_views.urls'
    97104
    class UpdateViewTests(TestCase): 
    224231        self.assertRedirects(res, 'http://testserver/list/authors/')
    225232        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
    226233
     234    def test_update_view_in_context(self):
     235        a = Author.objects.create(
     236            name='Randall Munroe',
     237            slug='randall-munroe',
     238        )
     239        res = self.client.get('/edit/author/%d/update/' % a.pk)
     240        self.assertEqual(res.status_code, 200)
     241        self.assertTrue(isinstance(res.context['view'], View))
     242
     243
    227244class DeleteViewTests(TestCase):
    228245    urls = 'regressiontests.generic_views.urls'
    229246
  • tests/regressiontests/generic_views/list.py

    diff --git a/tests/regressiontests/generic_views/list.py b/tests/regressiontests/generic_views/list.py
    index 9ad00ed..d707b06 100644
    a b from __future__ import absolute_import 
    22
    33from django.core.exceptions import ImproperlyConfigured
    44from django.test import TestCase
     5from django.views.generic.base import View
    56
    67from .models import Author, Artist
    78
    class ListViewTests(TestCase): 
    159160    def test_missing_items(self):
    160161        self.assertRaises(ImproperlyConfigured, self.client.get, '/list/authors/invalid/')
    161162
     163    def test_view_in_context(self):
     164        res = self.client.get('/list/authors/')
     165        self.assertEqual(res.status_code, 200)
     166        self.assertTrue(isinstance(res.context['view'], View))
     167
    162168    def _make_authors(self, n):
    163169        Author.objects.all().delete()
    164170        for i in range(n):
Back to Top