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

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

问题描述

这与这个问题有关:Django return 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.

简单的json

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

在 Django 1.6 及更早版本中,返回响应的代码是

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

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

:simplejson 已从 django 中删除,您可以使用:

import json

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

或者你可以使用 django.core.serializers 如上所述.

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

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

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