Skip to content

Instantly share code, notes, and snippets.

@revolunet
Created July 15, 2014 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revolunet/42e5704a6b69baf70c66 to your computer and use it in GitHub Desktop.
Save revolunet/42e5704a6b69baf70c66 to your computer and use it in GitHub Desktop.
django CBV minimal JsonMixin
# 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