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::
|
617 | 617 | """ |
618 | 618 | A mixin that can be used to render a JSON response. |
619 | 619 | """ |
620 | | response_class = HttpResponse |
621 | | |
622 | | def render_to_response(self, context, **response_kwargs): |
| 620 | def render_to_json_response(self, context, **response_kwargs): |
623 | 621 | """ |
624 | 622 | Returns a JSON response, transforming 'context' to make the payload. |
625 | 623 | """ |
626 | | response_kwargs['content_type'] = 'application/json' |
627 | | return self.response_class( |
| 624 | return HttpResponse( |
628 | 625 | self.convert_context_to_json(context), |
| 626 | content_type='application/json', |
629 | 627 | **response_kwargs |
630 | 628 | ) |
631 | 629 | |
… |
… |
For example, a simple JSON mixin might look something like this::
|
637 | 635 | # -- can be serialized as JSON. |
638 | 636 | return json.dumps(context) |
639 | 637 | |
640 | | Now we mix this into the base TemplateView:: |
| 638 | This mixin provides a `render_to_json_response` method with the same signature |
| 639 | as :func:`~django.views.generic.base.TemplateResponseMixin.render_to_response()`. |
| 640 | To use it, we simply need to mix it into a ``TemplateView`` for example, |
| 641 | and override ``render_to_response`` to call ``render_to_json_response`` instead:: |
641 | 642 | |
642 | 643 | from django.views.generic import TemplateView |
643 | 644 | |
644 | 645 | 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) |
646 | 648 | |
647 | 649 | Equally we could use our mixin with one of the generic views. We can make our |
648 | 650 | own version of :class:`~django.views.generic.detail.DetailView` by mixing |
… |
… |
rendering behavior has been mixed in)::
|
654 | 656 | from django.views.generic.detail import BaseDetailView |
655 | 657 | |
656 | 658 | 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) |
658 | 661 | |
659 | 662 | This view can then be deployed in the same way as any other |
660 | 663 | :class:`~django.views.generic.detail.DetailView`, with exactly the |
… |
… |
in both the ``JSONResponseMixin`` and a
|
668 | 671 | :class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`, |
669 | 672 | and override the implementation of |
670 | 673 | :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:: |
| 674 | to defer to the appropriate rendering method depending on the type of response |
| 675 | that the user requested:: |
673 | 676 | |
674 | 677 | from django.views.generic.detail import SingleObjectTemplateResponseMixin |
675 | 678 | |
676 | 679 | class HybridDetailView(JSONResponseMixin, SingleObjectTemplateResponseMixin, BaseDetailView): |
677 | 680 | def render_to_response(self, context): |
678 | 681 | # 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) |
681 | 684 | else: |
682 | | return SingleObjectTemplateResponseMixin.render_to_response(self, context) |
| 685 | return super(HybridDetailView, self).render_to_response(context) |
683 | 686 | |
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`. |
| 687 | Because of the way that Python resolves method overloading, the call to |
| 688 | ``super(HybridDetailView, self).render_to_response(context)`` ends up |
| 689 | calling the |
| 690 | :meth:`~django.views.generic.base.TemplateResponseMixin.render_to_response` |
| 691 | implementation of :class:`~django.views.generic.base.TemplateResponseMixin`. |