有效的url配置和视图的Django NoReverseMatch错误

时间:2013-07-15 21:42:08

标签: python django

我在模板渲染中遇到NoReverseMatch错误。

以下是相关模板:

<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 %}

错误是在网址'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)))

和urls.py的部分

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

2 个答案:

答案 0 :(得分:1)

你在delete_comment视图中的反向调用中向模板传递了两个参数; args=(project.url_path, report.id)但你的urls.py列出了;

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

只能接受一个参数。

答案 1 :(得分:0)

更改您的urls.py,为您的删除评论网址添加name参数。

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

然后尝试在模板中使用它;

{% url 'delete_comment' comment.id %}

请参阅naming URL patternsreverse resolution of URLs