2013-03-20 86 views
2

我对Django相当陌生,特别是CBV的。所以,我有一个post方法,在这里我想输出一个JSON一个ListView如下:simplejson返回在Django中的HttpResponse 1.5 CBV

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) 

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

回答

5

dump是倾销到一个文件,你想要dumps

+0

太棒了..就这样浪费了几个小时!再次感谢 - 我现在已经接受你的回答 – AJW 2013-03-20 22:38:29

+0

也,你想使用python的json,而不是simplejson for django 1.5 – dtc 2013-07-02 23:49:04

相关问题