【问题标题】:simplejson error on return HttpResponse in django 1.5 CBV在 django 1.5 CBV 中返回 HttpResponse 时的 simplejson 错误
【发布时间】:2013-03-10 06:55:38
【问题描述】:

我对 Django 很陌生,尤其是 CBV。因此,我有一个带有 post 方法的 listView,我试图在其中输出 JSON,如下所示:

from django.utils import simplejson

class MyCoolListView(ListView):
   # template declaration and other stuff

    def get_context_data(self, **kwargs):
       # do some stuff
       return context

    def get_queryset(self):
       # do some stuff
       return queryset 

    def post( self, request, *args, **kwargs ):
       # check if user is authenticated and return json
       return HttpResponse( simplejson.dump({ "score": blogpost.score }) , content_type='application/json')

但是,POST 上的 HttpResponse,我得到:

TypeError: dump() takes at least 2 arguments (1 given)

我不完全确定我做错了什么(我已经用谷歌搜索了很多这个问题,但还没有运气) - 我想知道是否有人遇到过这种情况/错误消息。任何解决此问题的指导将不胜感激。

【问题讨论】:

    标签: django json django-views simplejson


    【解决方案1】:

    dump 用于转储到文件,您需要dumps

    【讨论】:

    • 太棒了.. 只是在这上面浪费了几个小时!再次感谢 - 我现在已经接受了你的回答
    • 另外,你想使用python的json,而不是django 1.5的simplejson
    猜你喜欢
    • 1970-01-01
    • 2011-09-21
    • 2011-12-28
    • 2018-09-05
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 2020-04-17
    • 2018-10-11
    相关资源
    最近更新 更多