mirskutils.shortcuts¶
json responses¶
- mirskutils.shortcuts.json_render(request, template_or_data, template_data={}, json_data={}, httpresponse=<class 'django.http.response.HttpResponse'>)¶
- Description:
- respond to ajax request with json data (dictionary, list, string, etc) as the second argument, provide either:
- Arugments:
- template_or_data : (either a django.templates.Template object or a string of an existing template)
- template_data : used as context, if first argument is a template, to render the template. used in addition to the request context
- json_data : additional json information if template provided
- httpresponse : a django http response class, defaults to HttpResponse (200)
- Usage:
python:
def get(self, request): return json_response(request, 'tasks/list.html', {'list':items, 'name':'My List'}, {'status':'ok'})
javascript:
$.post('/api/list', function(data) { console.log(data); })
>>> { 'html' : < rendered template>, 'status':'ok' }
- mirskutils.shortcuts.json_response(request, data, httpresponse=<class 'django.http.response.HttpResponse'>)¶
creates http response with a json data type with the provided data
- mirskutils.shortcuts.jsonp_response(request, data, httpresponse=<class 'django.http.response.HttpResponse'>)¶
corollary to json_response but requires the request to have a callback