Render JSON with Django REST framework like TemplateHTMLRenderer

212 views
Skip to first unread message

Christopher Kim

unread,
Oct 15, 2015, 1:00:03 PM10/15/15
to Django REST framework

How can you use the same approach as the standard view way of rendering HTML templates? In the standard way you can do something like this:


from django.views.generic import TemplateView


class BaseView(TemplateView):
    template_name = 'something.html'

    def get_context_data(self, **kwargs):
        context = super(BaseView, self).get_context_data(**kwargs)
        context['body'] = 'lorem ipsum'
        return context


Can you have an APIView class with template_name set to something like 'something.json'? And then .json template file will have some context placeholders like {{ blah }}. But this class will still respond with json as the output, and you can also set format=json so that you can access the url with the .json extension. My default renderer class is rest_framework.renderers.JSONRenderer

Xavier Ordoquy

unread,
Oct 15, 2015, 1:12:41 PM10/15/15
to django-res...@googlegroups.com
Hi,

By overloading the functions, you are free to alter the serializer’s result before it gets passed to the renderer.

Regards,
Xavier,
Linovia.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages