Jimmy Jimmy - 5 months ago 118
JSON Question

Django view returning json without using template

This related to this question: Django return json and html depending on client python

I have a command line python api for a django app. When I access the app through the api it should return json and with a browser it should return html. I can use different urls to access the different versions but how do I render the html template and json in the views.py with just one template?

To render the html I would use:

return render_to_response('sample/sample.html....)


But how would I do the same for json without putting a json template? (the content-type should be application/json instead of text/html)

Edit 1:

What would determine the json and html outputs?

So in my views:

if something:
return render_to_response('html_template',.....)
else:
return HttpReponse(jsondata,mimetype='application/json')


Thank you

Answer

I think the issue has gotten confused regarding what you want. I imagine you're not actually trying to put the HTML in the JSON response, but rather want to alternatively return either HTML or JSON.

First, you need to understand the core difference between the two. HTML is a presentational format. It deals more with how to display data than the data itself. JSON is the opposite. It's pure data -- basically a JavaScript representation of some Python (in this case) dataset you have. It serves as merely an interchange layer, allowing you to move data from one area of your app (the view) to another area of your app (your JavaScript) which normally don't have access to each other.

With that in mind, you don't "render" JSON, and there's no templates involved. You merely convert whatever data is in play (most likely pretty much what you're passing as the context to your template) to JSON. Which can be done via either Django's JSON library (simplejson), if it's freeform data, or its serialization framework, if it's a queryset.

simplejson

from django.utils import simplejson

some_data_to_dump = {
   'some_var_1': 'foo',
   'some_var_2': 'bar',
}

data = simplejson.dumps(some_data_to_dump)

Serialization

from django.core import serializers

foos = Foo.objects.all()

data = serializers.serialize('json', foos)

Either way, you then pass that data into the response:

return HttpResponse(data, content_type='application/json')

[Edit] In Django 1.6 and earlier, the code to return response was

return HttpResponse(data, mimetype='application/json')
Comments