Ticket #16101: 16101.customisable-detailview-pk-slug-url-kwargs.diff

File 16101.customisable-detailview-pk-slug-url-kwargs.diff, 5.9 KB (added by Julien Phalip, 13 years ago)
  • django/views/generic/detail.py

    diff --git a/django/views/generic/detail.py b/django/views/generic/detail.py
    index 7e9047f..501f0ef 100644
    a b class SingleObjectMixin(object):  
    1515    queryset = None
    1616    slug_field = 'slug'
    1717    context_object_name = None
     18    slug_url_kwarg = 'slug'
     19    pk_url_kwarg = 'pk'
    1820
    1921    def get_object(self, queryset=None):
    2022        """
    class SingleObjectMixin(object):  
    2931            queryset = self.get_queryset()
    3032
    3133        # Next, try looking up by primary key.
    32         pk = self.kwargs.get('pk', None)
    33         slug = self.kwargs.get('slug', None)
     34        pk = self.kwargs.get(self.pk_url_kwarg, None)
     35        slug = self.kwargs.get(self.slug_url_kwarg, None)
    3436        if pk is not None:
    3537            queryset = queryset.filter(pk=pk)
    3638
  • docs/ref/class-based-views.txt

    diff --git a/docs/ref/class-based-views.txt b/docs/ref/class-based-views.txt
    index e9f3152..132913f 100644
    a b SingleObjectMixin  
    136136        The name of the field on the model that contains the slug. By default,
    137137        ``slug_field`` is ``'slug'``.
    138138
     139    .. attribute:: slug_url_kwarg
     140
     141        The name of the URLConf keyword argument that contains the slug. By
     142        default, ``slug_url_kwarg`` is ``'slug'``.
     143
     144    .. attribute:: pk_url_kwarg
     145
     146        The name of the URLConf keyword argument that contains the primary key.
     147        By default, ``pk_url_kwarg`` is ``'pk'``.
     148
    139149    .. attribute:: context_object_name
    140150
    141151        Designates the name of the variable to use in the context.
    SingleObjectMixin  
    146156        ``queryset`` is provided, that queryset will be used as the
    147157        source of objects; otherwise,
    148158        :meth:`~SingleObjectMixin.get_queryset` will be used.
    149         :meth:`~SingleObjectMixin.get_object` looks for a ``pk``
    150         argument in the arguments to the view; if ``pk`` is found,
    151         this method performs a primary-key based lookup using that
    152         value. If no ``pk`` argument is found, it looks for a ``slug``
     159        ``get_object()`` looks for a
     160        :attr:`SingleObjectMixin.pk_url_kwarg` argument in the arguments
     161        to the view; if this argument is found, this method performs a
     162        primary-key based lookup using that value. If this argument is not
     163        found, it looks for a :attr:`SingleObjectMixin.slug_url_kwarg`
    153164        argument, and performs a slug lookup using the
    154165        :attr:`SingleObjectMixin.slug_field`.
    155166
  • docs/releases/1.4.txt

    diff --git a/docs/releases/1.4.txt b/docs/releases/1.4.txt
    index 7fdf0d7..d7843ba 100644
    a b A new helper function,  
    7878``template.Library`` to ease the creation of template tags that store some
    7979data in a specified context variable.
    8080
     81Customizable ``SingleObjectMixin`` URLConf kwargs
     82~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     83
     84Two new attributes,
     85:attr:`pk_url_kwarg<django.views.generic.detail.SingleObjectMixin.pk_url_kwarg>` and
     86:attr:`slug_url_kwarg<django.views.generic.detail.SingleObjectMixin.slug_url_kwarg>`,
     87have been added to :class:`django.views.generic.detail.SingleObjectMixin` to
     88enable the customization of URLConf keyword arguments used for single
     89object generic views.
     90
    8191CSRF improvements
    8292~~~~~~~~~~~~~~~~~
    8393
  • tests/regressiontests/generic_views/detail.py

    diff --git a/tests/regressiontests/generic_views/detail.py b/tests/regressiontests/generic_views/detail.py
    index e8f2050..cd55d26 100644
    a b class DetailViewTest(TestCase):  
    2121        self.assertEqual(res.context['author'], Author.objects.get(pk=1))
    2222        self.assertTemplateUsed(res, 'generic_views/author_detail.html')
    2323
     24    def test_detail_by_custom_pk(self):
     25        res = self.client.get('/detail/author/bycustompk/1/')
     26        self.assertEqual(res.status_code, 200)
     27        self.assertEqual(res.context['object'], Author.objects.get(pk=1))
     28        self.assertEqual(res.context['author'], Author.objects.get(pk=1))
     29        self.assertTemplateUsed(res, 'generic_views/author_detail.html')
     30
    2431    def test_detail_by_slug(self):
    2532        res = self.client.get('/detail/author/byslug/scott-rosenberg/')
    2633        self.assertEqual(res.status_code, 200)
    2734        self.assertEqual(res.context['object'], Author.objects.get(slug='scott-rosenberg'))
    2835        self.assertEqual(res.context['author'], Author.objects.get(slug='scott-rosenberg'))
    2936        self.assertTemplateUsed(res, 'generic_views/author_detail.html')
     37   
     38    def test_detail_by_custom_slug(self):
     39        res = self.client.get('/detail/author/bycustomslug/scott-rosenberg/')
     40        self.assertEqual(res.status_code, 200)
     41        self.assertEqual(res.context['object'], Author.objects.get(slug='scott-rosenberg'))
     42        self.assertEqual(res.context['author'], Author.objects.get(slug='scott-rosenberg'))
     43        self.assertTemplateUsed(res, 'generic_views/author_detail.html')
    3044
    3145    def test_verbose_name(self):
    3246        res = self.client.get('/detail/artist/1/')
  • tests/regressiontests/generic_views/urls.py

    diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py
    index 067c1f6..dc86e4d 100644
    a b urlpatterns = patterns('',  
    3030    url(r'^detail/author/(?P<pk>\d+)/$',
    3131        views.AuthorDetail.as_view(),
    3232        name="author_detail"),
     33    (r'^detail/author/bycustompk/(?P<foo>\d+)/$',
     34        views.AuthorDetail.as_view(pk_url_kwarg='foo')),                                             
    3335    (r'^detail/author/byslug/(?P<slug>[\w-]+)/$',
    3436        views.AuthorDetail.as_view()),
     37    (r'^detail/author/bycustomslug/(?P<foo>[\w-]+)/$',
     38        views.AuthorDetail.as_view(slug_url_kwarg='foo')),                       
    3539    (r'^detail/author/(?P<pk>\d+)/template_name_suffix/$',
    3640        views.AuthorDetail.as_view(template_name_suffix='_view')),
    3741    (r'^detail/author/(?P<pk>\d+)/template_name/$',
Back to Top