Created
December 4, 2014 14:38
-
-
Save heyman/eec08ec0ed81df205e83 to your computer and use it in GitHub Desktop.
Django TemplateView with support for returning HttpResponse in get_context_data()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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