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

时间:2020-12-04 19:56:48

I'm getting a NoReverseMatch error in my template rendering.

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

Here's the relevant template:

这是相关的模板:

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

The error is given on the url 'mokr.delete_comment' line

错误在url'mokr.delete_comment'行上给出

Here's the view:

这是观点:

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)))

and the section of urls.py

和urls.py的部分

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

2 个解决方案

#1


0  

Alter your urls.py to add a name argument to your delete comment url.

更改urls.py,为删除注释网址添加名称参数。

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

Then try using this in your template;

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

{% url 'delete_comment' comment.id %}

See naming URL patterns and reverse resolution of URLs

请参阅命名URL模式和反向URL解析

#2


1  

You're passing two arguments to the template in your call to reverse in the delete_comment view; args=(project.url_path, report.id) but your urls.py lists;

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

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

Which can only accept one parameter.

哪个只能接受一个参数。

#1


0  

Alter your urls.py to add a name argument to your delete comment url.

更改urls.py,为删除注释网址添加名称参数。

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

Then try using this in your template;

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

{% url 'delete_comment' comment.id %}

See naming URL patterns and reverse resolution of URLs

请参阅命名URL模式和反向URL解析

#2


1  

You're passing two arguments to the template in your call to reverse in the delete_comment view; args=(project.url_path, report.id) but your urls.py lists;

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

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

Which can only accept one parameter.

哪个只能接受一个参数。