Django视图返回json而不使用模板 [英] Django view returning json without using template

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

问题描述

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



我有一个django应用程序的命令行python api。当我通过api访问应用程序时,应该返回json并使用浏览器返回html。我可以使用不同的URL来访问不同的版本,但是如何使用一个模板在views.py中呈现html模板和json?



要渲染HTML将使用:

 返回render_to_response('sample / sample.html ....)

但是,如果没有放置json模板,我该怎么做同样的json? (内容类型应该是application / json而不是text / html)



编辑1:



什么将确定json和html输出?



所以在我的意见中:

 如果有的话:
return render_to_response('html_template',.....)
else:
return HttpReponse(jsondata,mimetype ='application / json')

谢谢

解决方案

我认为这个问题对你的想法感到困惑。我想象你实际上并没有将HTML放在JSON响应中,而是希望替换为HTML或JSON。



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



考虑到这一点,您不要渲染JSON,并且没有涉及模板。您只需将JSON中的任何数据转换为JSON(很可能几乎是您作为上下文传递给您的模板)。可以通过Django的JSON库(simplejson)完成,如果它是自由格式数据,或者它的序列化框架,如果它是一个查询器。



simplejson

 从django.utils import simplejson 

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

data = simplejson.dumps(some_data_to_dump)
pre>

序列化

  from django .core import serializers 

foos = Foo.objects.all()

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

无论哪种方式,您都可以将该数据传递给响应:

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

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

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


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

解决方案

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

这篇关于Django视图返回json而不使用模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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