92

This is 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)

What would determine the JSON and HTML outputs?

So in my views.py:

if something:
    return render_to_response('html_template',.....)
else:
    return HttpReponse(jsondata,mimetype='application/json')
daaawx
  • 3,273
  • 2
  • 17
  • 16
Neeran
  • 1,753
  • 3
  • 21
  • 26
  • @Marcin You basically told him "No, don't do it this way" without showing him an example of the right way. That's what this one seems to be for... – Izkata Feb 13 '12 at 14:54
  • @Jimmy, if that's what happened, you shouldn't have accepted Marcin's answer on the other question so quickly. Wait at least a day, someone likely would have answered with something like Uku Loskit's answer – Izkata Feb 13 '12 at 14:55
  • @Izkata: I did actually tell him which library to use. This question appears to be for the purpose of getting someone else to write his code for him. – Marcin Feb 13 '12 at 14:59

9 Answers9

144

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')

[EDIT]: simplejson was remove from django, you can use:

import json

json.dumps({"foo": "bar"})

Or you can use the django.core.serializers as described above.

oz123
  • 27,559
  • 27
  • 125
  • 187
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Thank you for clarification. How do I determine in my views that the response request is by the api for the json? See edit on question. – Neeran Feb 13 '12 at 16:39
  • 1
    You can use `request.is_ajax()`. But that requires that the `HTTP_X_REQUESTED_WITH` header is set. Most JavaScript libraries do this automatically, but if you're using some other type of client, you'll need to make sure it sets it as well. Alternatively, you can pass a querystring such as `?json` with the URL and then check `request.GET.has_key('json')`, which is probably a little more foolproof. – Chris Pratt Feb 13 '12 at 16:41
  • 19
    Note that simplejson is now considered [deprecated by Django 1.5](https://docs.djangoproject.com/en/dev/releases/1.5/#system-version-of-simplejson-no-longer-used). Use `import json ; json.dumps(data)` instead. – Yonatan Apr 29 '13 at 23:59
  • 1
    The OP should check the "Accept" content type negotiation header in the `request` object. See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html (big mamoth of a read, but a simplified code sample could be used to demonstrate, and it wouldn't be very hard to write an inflexible system that would at least handle the two cases they're asking) – Merlyn Morgan-Graham Aug 20 '13 at 06:41
  • 14
    In my case (Django 1.7) it was content_type='application/json', not mimetype – Nopik Oct 13 '14 at 16:05
133

In Django 1.7 this is even easier with the built-in JsonResponse.

https://docs.djangoproject.com/en/dev/ref/request-response/#jsonresponse-objects

# import it
from django.http import JsonResponse

def my_view(request):

    # do something with the your data
    data = {}

    # just return a JsonResponse
    return JsonResponse(data)
davegaeddert
  • 3,139
  • 1
  • 21
  • 20
8

For rendering my models in JSON in django 1.9 I had to do the following in my views.py:

from django.core import serializers
from django.http import HttpResponse
from .models import Mymodel

def index(request):
    objs = Mymodel.objects.all()
    jsondata = serializers.serialize('json', objs)
    return HttpResponse(jsondata, content_type='application/json')
Ahsan
  • 3,845
  • 2
  • 36
  • 36
  • You can use [JsonResponse](https://docs.djangoproject.com/en/2.2/ref/request-response/#django.http.JsonResponse) – MichielB Sep 11 '19 at 13:03
8

In the case of the JSON response there is no template to be rendered. Templates are for generating HTML responses. The JSON is the HTTP response.

However, you can have HTML that is rendered from a template withing your JSON response.

html = render_to_string("some.html", some_dictionary)
serialized_data = simplejson.dumps({"html": html})
return HttpResponse(serialized_data, mimetype="application/json")
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
7

It looks like the Django REST framework uses the HTTP accept header in a Request in order to automatically determine which renderer to use:

http://www.django-rest-framework.org/api-guide/renderers/

Using the HTTP accept header may provide an alternative source for your "if something".

Charles Brandt
  • 981
  • 10
  • 13
2
from django.utils import simplejson 
from django.core import serializers 

def pagina_json(request): 
   misdatos = misdatos.objects.all()
   data = serializers.serialize('json', misdatos) 
   return HttpResponse(data, mimetype='application/json')
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
2

Here's an example I needed for conditionally rendering json or html depending on the Request's Accept header

# myapp/views.py
from django.core import serializers                                                                                
from django.http import HttpResponse                                                                                  
from django.shortcuts import render                                                                                   
from .models import Event

def event_index(request):                                                                                             
    event_list = Event.objects.all()                                                                                  
    if request.META['HTTP_ACCEPT'] == 'application/json':                                                             
        response = serializers.serialize('json', event_list)                                                          
        return HttpResponse(response, content_type='application/json')                                                
    else:                                                                                                             
        context = {'event_list': event_list}                                                                          
        return render(request, 'polls/event_list.html', context)

you can test this with curl or httpie

$ http localhost:8000/event/
$ http localhost:8000/event/ Accept:application/json

note I opted not to use JsonReponse as that would reserialize the model unnecessarily.

Harry Moreno
  • 10,231
  • 7
  • 64
  • 116
2

You could also check the request accept content type as specified in the rfc. That way you can render by default HTML and where your client accept application/jason you can return json in your response without a template being required

Greg
  • 101
  • 6
0

If you want to pass the result as a rendered template you have to load and render a template, pass the result of rendering it to the json.This could look like that:

from django.template import loader, RequestContext

#render the template
t=loader.get_template('sample/sample.html')
context=RequestContext()
html=t.render(context)

#create the json
result={'html_result':html)
json = simplejson.dumps(result)

return HttpResponse(json)

That way you can pass a rendered template as json to your client. This can be useful if you want to completely replace ie. a containing lots of different elements.

marue
  • 5,588
  • 7
  • 37
  • 65
  • 1
    As a side note, `render_to_string` is a shortcut for the 3 "render the template" lines, and has existed since Django 1.0 – Izkata Feb 13 '12 at 14:59