如何在Django中不使用模板的情况下返回JSON? [英] How do I return JSON without using a template in Django?

查看:137
本文介绍了如何在Django中不使用模板的情况下返回JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与此问题有关: Django返回json和html取决于客户端python

我有一个用于Django应用的命令行Python API.当我通过API访问该应用程序时,它应该返回JSON,而在浏览器中它应该返回HTML.我可以使用不同的URL访问不同的版本,但是如何仅用一个模板在 views.py 中呈现HTML模板和JSON?

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?

要呈现HTML,我将使用:

To render the HTML I would use:

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

但是我如何在不放置JSON模板的情况下对JSON做同样的事情? (content-type应该是application/json而不是text/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)

什么将决定JSON和HTML输出?

What would determine the JSON and HTML outputs?

所以在我的 views.py 中:

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

推荐答案

我认为这个问题对您想要的东西感到困惑.我想您实际上不是在尝试将HTML放入JSON响应中,而是想要替代地返回HTML或JSON.

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.

首先,您需要了解两者之间的核心区别. HTML是一种表示形式.与数据本身相比,它更多地涉及如何显示数据. JSON相反.它是纯数据-基本上是您拥有的某些Python(在这种情况下)数据集的JavaScript表示形式.它只是一个交换层,使您可以将数据从应用程序的一个区域(视图)移动到应用程序的另一区域(您的JavaScript),而这些区域通常是无法相互访问的.

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.

请记住,您无需渲染" JSON,并且不涉及任何模板.您只需将正在使用的所有数据(很可能是您作为上下文传递到模板的数据)转换为JSON.如果是自由格式数据,则可以通过Django的JSON库(simplejson)进行;如果是查询集,则可以通过序列化框架来完成.

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)

序列化

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]在Django 1.6及更早版本中,返回响应的代码为

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

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

这篇关于如何在Django中不使用模板的情况下返回JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆