Skip to content

Instantly share code, notes, and snippets.

@heyman
Created December 4, 2014 14:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save heyman/eec08ec0ed81df205e83 to your computer and use it in GitHub Desktop.
Save heyman/eec08ec0ed81df205e83 to your computer and use it in GitHub Desktop.
Django TemplateView with support for returning HttpResponse in get_context_data()
from django.http import HttpResponse
from django.views import generic
class TemplateView(generic.TemplateView):
"""
Exactly like Django's TemplateView, but adds support for returning an
HttpResponse in get_context_data
"""
def get(self, request, *args, **kwargs):
context_or_response = self.get_context_data(**kwargs)
if isinstance(context_or_response, HttpResponse):
return context_or_response
else:
return self.render_to_response(context_or_response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment