<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>django &amp;mdash; Nat Knight</title>
    <link>http://natknight.xyz/tag:django</link>
    <description>Reflections, diversions, and opinions from a progressive ex-physicist programmer dad with a sore back.</description>
    <pubDate>Sat, 23 May 2026 14:46:36 -0700</pubDate>
    <item>
      <title>TIL: Making a Django REST framework view do server-side rendering</title>
      <link>http://natknight.xyz/til-making-a-django-rest-framework-view-do-server-side-rendering</link>
      <description>&lt;![CDATA[#til #django #djangorestramework #python&#xA;&#xA;On a recent project I found myself needing one classic form-and-template style page in an otherwise API-driven project. I could, of course, [just do it] with a regular view function, but I had a bunch of authentication and suchlike set up for DRF APIViews.&#xA;&#xA;Turns out it&#39;s actually pretty easy to make an APIView kick it oldschool!&#xA;&#xA;!--more--&#xA;&#xA;from restframework.parsers import FormParser&#xA;from restframework.renderers import TemplateHTMLRenderer&#xA;from restframework.response import Response&#xA;from restframework.views import APIView&#xA;&#xA;class OldSchoolView(APIView):&#xA;    parserclasses = [FormParser]&#xA;    rendererclasses = [TemplateHTMLRenderer]&#xA;    template_name = &#34;path/to/a/template.html&#34;&#xA;&#xA;    def get(self, request):&#xA;        context = {}&#xA;        # do regular view stuff here&#xA;        return Response(context)&#xA;&#xA;    def post(self, request):&#xA;        formdata = request.data.dict()&#xA;        # do regular view stuff here&#xA;        return Response()  # or redirect; whatever&#39;s appropriate&#xA;&#xA;The indicated template gets rendered with the context you pass to the Response.&#xA;&#xA;I think you could write a view that simultaneously supports HTML and JSON with [content negotiation], but it ended up being more trouble that it was worth in my case.&#xA;&#xA;[just do it]: https://spookylukey.github.io/django-views-the-right-way/anything.html]]&gt;</description>
      <content:encoded><![CDATA[<p><a href="http://natknight.xyz/tag:til" class="hashtag"><span>#</span><span class="p-category">til</span></a> <a href="http://natknight.xyz/tag:django" class="hashtag"><span>#</span><span class="p-category">django</span></a> <a href="http://natknight.xyz/tag:djangorestramework" class="hashtag"><span>#</span><span class="p-category">djangorestramework</span></a> <a href="http://natknight.xyz/tag:python" class="hashtag"><span>#</span><span class="p-category">python</span></a></p>

<p>On a recent project I found myself needing one classic form-and-template style page in an otherwise API-driven project. I could, of course, <a href="https://spookylukey.github.io/django-views-the-right-way/anything.html">just do it</a> with a regular view function, but I had a bunch of authentication and suchlike set up for DRF <code>APIView</code>s.</p>

<p>Turns out it&#39;s actually pretty easy to make an <code>APIView</code> kick it oldschool!</p>



<pre><code class="language-python">from rest_framework.parsers import FormParser
from rest_framework.renderers import TemplateHTMLRenderer
from rest_framework.response import Response
from rest_framework.views import APIView


class OldSchoolView(APIView):
    parser_classes = [FormParser]
    renderer_classes = [TemplateHTMLRenderer]
    template_name = &#34;path/to/a/template.html&#34;

    def get(self, request):
        context = {}
        # do regular view stuff here
        return Response(context)

    def post(self, request):
        formdata = request.data.dict()
        # do regular view stuff here
        return Response()  # or redirect; whatever&#39;s appropriate
</code></pre>

<p>The indicated template gets rendered with the <code>context</code> you pass to the <code>Response</code>.</p>

<p>I think you could write a view that simultaneously supports HTML and JSON with <a href="https://www.django-rest-framework.org/api-guide/content-negotiation/">content negotiation</a>, but it ended up being more trouble that it was worth in my case.</p>
]]></content:encoded>
      <guid>http://natknight.xyz/til-making-a-django-rest-framework-view-do-server-side-rendering</guid>
      <pubDate>Thu, 06 Feb 2025 08:06:18 +0000</pubDate>
    </item>
  </channel>
</rss>