Created
July 15, 2014 22:27
-
-
Save revolunet/42e5704a6b69baf70c66 to your computer and use it in GitHub Desktop.
django CBV minimal JsonMixin
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
# urls.py | |
urlpatterns += patterns('', url('^test$', views.MyView.as_view())) | |
# views.py | |
import json | |
from django.http import HttpResponse | |
class JsonMixin(object): | |
http_allowed_methods = ['get'] | |
def get(self, request, *args, **kwargs): | |
data = self.get_context_data() | |
json_data = json.dumps(data) | |
return HttpResponse( | |
json_data, | |
content_type='application/json' | |
) | |
class MyView(JsonMixin, View): | |
def get_context_data(self, **kwargs): | |
return { | |
'hello': 'world', | |
'success': True | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment