2013-07-15 30 views
1

我在我的模板呈現中收到NoReverseMatch錯誤。Django NoReverseMatch錯誤,有效的URL配置和視圖

下面是相關的模板:

<ul id='comments'> 
{% for comment in comments %} 
<li class='comment'> 
    <img class='gravatar' src='{{ comment.User|gravatar:50}}' alt='{{ comment.User.get_full_name }}' \> 
    <a href='{% url 'dashboard.views.users.profile' comment.User.id %}' class='user'> 
     {{comment.User.get_full_name}} 
    </a> 

    <p class='comment-timestamp'>{{comment.created}}</p> 
    <p class='comment-content'>{{comment.comment|striptags}}<br> 
    {% if user == comment.user or user = report.user %} 
    <a href="{% url 'mokr.delete_comment' comment.id %}">Delete</a></p> 
    {% endif %} 
</li> 

{% endfor %} 

該錯誤的URL 'mokr.delete_comment' 線

給這裏的觀點:

def delete_comment(request, comment_id): 

    comment = get_object_or_404(ReportComment, id = comment_id) 
    report = comment.MgmtReport 
    comment.delete() 

    project = report.project 

    return HttpResponseRedirect(reverse('show_post', args=(project.url_path, report.id))) 

和網址的部分。 py

(r'^mokr/comment/(\d+)/delete/$', mokr.delete_comment), 
url(r'^mokr/show/([^\.]*)/(\d+)/$', mokr.show, name='show_post'), 
+0

你永遠不叫你的網址,你需要'name ='在你的URL模式刪除,comment''。 –

回答

0

改變你的urls.py爲你的刪除評論url添加一個name參數。

(r'^mokr/comment/(\d+)/delete/$', mokr.delete_comment, name="delete_comment"), 

然後嘗試在您的模板中使用它;

{% url 'delete_comment' comment.id %} 

naming URL patternsreverse resolution of URLs

+1

這樣做!非常感謝您的幫助 – user2562438

1

您正在向調用中的模板傳遞兩個參數以在delete_comment視圖中進行反轉; args=(project.url_path, report.id)但你的urls.py列表;

(r'^mokr/comment/(\d+)/delete/$', mokr.delete_comment), 

這隻能接受一個參數。

+0

此時,我正在調用另一個函數「顯示報告」功能。我將編輯添加這部分的urls.py。 除非我不理解逆向方法如何正常工作? – user2562438

+0

@ user2562438對不起! mokr是一個命名空間嗎?也許試試'{%url'mokr:delete_comment'comment.id%}'? –

+0

的意圖是,我有一個報告,其中有一個評論。如果刪除評論按鈕被擊中,則評論被刪除,然後在show方法上運行反向,將用戶返回到帖子的頁面 – user2562438