'HttpResponse'对象没有属性'get_absolute_url'

时间:2015-10-13 12:38:15

标签: python django views

在我们的Q / A网站上,评论突然停止了。在尝试发表评论时,我得到以下错误:

Traceback:
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)
File "/app/src/inteokej/installed_apps/QAManager/views/threads.py" in my_post_comment
  98.         return redirect(comment.get_absolute_url())

Exception Type: AttributeError at /comments/post/
Exception Value: 'HttpResponse' object has no attribute 'get_absolute_url'

Read the full Traceback

以下是视图中的评论代码:

def my_post_comment(request,next=None,using=None):
    (model, target)=get_comment_target(request)
    user=request.user
    if not user.has_perm(PERM_APP+'can_comment'):
        if model==Question and target.user==request.user:
            if not user.has_perm(PERM_APP+"can_comment_own_question"):
                raise Exception('You cannot comment your own question')
        elif model==Answer and (target.user==request.user or target.question.user==request.user):
            if not user.has_perm(PERM_APP+"can_comment_own_answer"):
                raise Exception('You cannot comment your own answer')
        else:
            raise Exception('You cannot comment')

    comment=post_comment_from_form(request, model, target, next=None,using=None)
    #raise Exception(comment)

    if request.is_ajax():
        data=simplejson.dumps({'result':render_to_string('comments/list.html',{'comment_list':[comment]},context_instance=RequestContext(request))})
        return HttpResponse(data,mimetype='application/json')
    else:
        #return render_to_response('comments/list.html',{'comment_list':[comment]},context_instance=RequestContext(request))
        return redirect(comment.get_absolute_url())

这是什么意思?感谢

1 个答案:

答案 0 :(得分:1)

错误在这一行:

return redirect(comment.get_absolute_url())

它告诉您评论是HttpResponse,而不是Comment实例。因此,它没有get_absolute_url()方法。

由于评论是在这一行设置的:

comment=post_comment_from_form(request, model, target, next=None,using=None)

您需要查看post_comment_from_form方法。如果您要在此视图中使用该方法,则需要返回comment,而不是HttpResponse