Ticket #20643: ticket-20643.patch

File ticket-20643.patch, 4.1 KB (added by Baptiste Mispelon, 11 years ago)
  • docs/topics/class-based-views/mixins.txt

    diff --git a/docs/topics/class-based-views/mixins.txt b/docs/topics/class-based-views/mixins.txt
    index f13c468..61b0b00 100644
    a b For example, a simple JSON mixin might look something like this::  
    617617        """
    618618        A mixin that can be used to render a JSON response.
    619619        """
    620         response_class = HttpResponse
    621 
    622         def render_to_response(self, context, **response_kwargs):
     620        def render_to_json_response(self, context, **response_kwargs):
    623621            """
    624622            Returns a JSON response, transforming 'context' to make the payload.
    625623            """
    626             response_kwargs['content_type'] = 'application/json'
    627             return self.response_class(
     624            return HttpResponse(
    628625                self.convert_context_to_json(context),
     626                content_type='application/json',
    629627                **response_kwargs
    630628            )
    631629
    For example, a simple JSON mixin might look something like this::  
    637635            # -- can be serialized as JSON.
    638636            return json.dumps(context)
    639637
    640 Now we mix this into the base TemplateView::
     638This mixin provides a `render_to_json_response` method with the same signature
     639as :func:`~django.views.generic.base.TemplateResponseMixin.render_to_response()`.
     640To use it, we simply need to mix it into a ``TemplateView`` for example,
     641and override ``render_to_response`` to call ``render_to_json_response`` instead::
    641642
    642643    from django.views.generic import TemplateView
    643644
    644645    class JSONView(JSONResponseMixin, TemplateView):
    645         pass
     646        def render_to_response(self, context, **response_kwargs):
     647            return self.render_to_json_response(context, **response_kwargs)
    646648
    647649Equally we could use our mixin with one of the generic views. We can make our
    648650own version of :class:`~django.views.generic.detail.DetailView` by mixing
    rendering behavior has been mixed in)::  
    654656    from django.views.generic.detail import BaseDetailView
    655657
    656658    class JSONDetailView(JSONResponseMixin, BaseDetailView):
    657         pass
     659        def render_to_response(self, context, **response_kwargs):
     660            return self.render_to_json_response(context, **response_kwargs)
    658661
    659662This view can then be deployed in the same way as any other
    660663:class:`~django.views.generic.detail.DetailView`, with exactly the
    in both the ``JSONResponseMixin`` and a  
    668671:class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`,
    669672and override the implementation of
    670673:func:`~django.views.generic.base.TemplateResponseMixin.render_to_response()`
    671 to defer to the appropriate subclass depending on the type of response that the
    672 user requested::
     674to defer to the appropriate rendering method depending on the type of response
     675that the user requested::
    673676
    674677    from django.views.generic.detail import SingleObjectTemplateResponseMixin
    675678
    676679    class HybridDetailView(JSONResponseMixin, SingleObjectTemplateResponseMixin, BaseDetailView):
    677680        def render_to_response(self, context):
    678681            # Look for a 'format=json' GET argument
    679             if self.request.GET.get('format','html') == 'json':
    680                 return JSONResponseMixin.render_to_response(self, context)
     682            if self.request.GET.get('format') == 'json':
     683                return self.render_to_json_response(context)
    681684            else:
    682                 return SingleObjectTemplateResponseMixin.render_to_response(self, context)
     685                return super(HybridDetailView, self).render_to_response(context)
    683686
    684 Because of the way that Python resolves method overloading, the local
    685 ``render_to_response()`` implementation will override the versions provided by
    686 ``JSONResponseMixin`` and
    687 :class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`.
     687Because of the way that Python resolves method overloading, the call to
     688``super(HybridDetailView, self).render_to_response(context)`` ends up
     689calling the
     690:meth:`~django.views.generic.base.TemplateResponseMixin.render_to_response`
     691implementation of :class:`~django.views.generic.base.TemplateResponseMixin`.
Back to Top