2015-10-13 102 views
0

在我們的Q/A網站上,評論突然停止工作。當試圖發表評論我得到follwing錯誤:'HttpResponse'對象沒有屬性'get_absolute_url'

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

的錯誤是在這條線:

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