【问题标题】:Django view not returning the json data but does so at the shellDjango 视图不返回 json 数据,但在 shell 中返回
【发布时间】:2012-03-25 15:32:29
【问题描述】:

如果我启动开发服务器并转到某个视图。我没有得到回应。

我的看法是这样的

from django.http import HttpResponse, Http404
from goals.models import Child
import json

def viewChildren(request):
    jsonData= []
    allChildren = Child.objects.all()
    for child in allChildren:
        jsonData.append({'id': child.id, 'name': child.name})
    return HttpResponse(json.dumps(jsonData),mimetype="application/json")

为了测试发生了什么,我做了以下操作

python manage.py shell
>>> import goals.views as v
>>> r = v.viewChildren('')
>>> print r
Content-Type: application/json

[{"id": 1, "name": "Test Child"}, {"id": 2, "name": "Second child"}]

所以我知道视图是正确的,并且在调用视图后正在传递正确的数据...但是当我从浏览器查看时,这并没有被传递。

有什么建议吗? (这类似于很多问题,但我发现没有一个显示上述情况,即视图提供了正确的响应,但从浏览器却没有)

【问题讨论】:

  • 我还在 urls.py 中添加了正确的 url,其中包含以下内容:url(r'children/$', 'goals.views.viewChildren') 我正在尝试访问http://localhost:8080/children/ 的页面,管理页面工作正常

标签: python django json httpresponse


【解决方案1】:

刚刚解决了问题。我删除了所有其余的网址,因为我认为它们可能不正确。原来,先前的编辑使所有 url 转到一个空白索引,没有返回任何响应。修复后,上述工作正常。

感谢您的关注...

【讨论】:

  • 我想你可以接受这个答案
猜你喜欢
  • 1970-01-01
  • 2012-01-18
  • 2018-04-16
  • 2020-03-17
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-25
相关资源
最近更新 更多