gpt4 book ai didi

django - django 1.5 CBV 中返回 HttpResponse 时出现 simplejson 错误

转载 作者:行者123 更新时间:2023-12-02 22:09:37 24 4
gpt4 key购买 nike

我对 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)

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

最佳答案

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

关于django - django 1.5 CBV 中返回 HttpResponse 时出现 simplejson 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15536104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com