NoReverseMatch 在 /details/The-Shortest-Scientific-Papers-Ever-Published-fa9c94fc-626a-446a-8b34-e6ae

Posted

技术标签:

【中文标题】NoReverseMatch 在 /details/The-Shortest-Scientific-Papers-Ever-Published-fa9c94fc-626a-446a-8b34-e6aeddcc086f【英文标题】:NoReverseMatch at /details/The-Shortest-Scientific-Papers-Ever-Published-fa9c94fc-626a-446a-8b34-e6aeddcc086f 【发布时间】:2022-01-23 22:41:52 【问题描述】:

我正在尝试在我的项目博客中添加 cmets。 当我发表评论时,它在 /details/The-Shortest-Scientific-Papers-Ever-Published-fa9c94fc-626a-446a-8b34-e6aeddcc086f 显示 NoReverseMatch

这是博客详情页面

% extends 'base.html' %
% block content %
<div class="container jumbotron">
    <div class="row">
        <div class="col-sm-6">
            <h2><i>blog.blog_title</i></h2>
            <h4>Posted By: @ blog.author</h4>
            <i><h6>Published On : blog.publish_date</h6></i>
        </div>
        <div class="col-sm-6">
            <img src="/media/blog.blog_image"  title="blog.blog_title"  >
        </div><hr>
        <p>blog.blog_content|capfirst
    </div>
    <div class="row">
        <div class="col-sm-6">
            <hr>
            <h5>Comments*</h5>
        </div>
        <div class="col-sm-6">
            <form method="post">
                comment_form.as_p
                % csrf_token %
                <button type="submit" class="btn btn-primary btn-sm">Comment</button>
            </form>
        </div>
    </div>
</div>
% endblock content %

博客详情页面的views.py-

def blog_details(request, slug):
    blog= Blog.objects.get(slug=slug)
    commet_form=CommentForm()
    if request.method=='POST':
        commet_form=CommentForm(request.POST)
        if commet_form.is_valid():
            comment=commet_form.save(commit=False)
            comment.user=request.user
            comment.blog=blog
            comment.save()
            return redirect('blog_details',kwargs='slug':slug)
    return render(request,'blogs/blogdetails.html',context='blog':blog,'comment_form':commet_form)

博客详情页的urls.py映射

urlpatterns = [
    path('details/<slug:slug>',views.blog_details,name="blog_details"),
]

服务网址

http://localhost:8000/details/The-Shortest-Scientific-Papers-Ever-Published-fa9c94fc-626a-446a-8b34-e6aeddcc086f

但是在服务器站点我得到了这个

NoReverseMatch at /details/The-Shortest-Scientific-Papers-Ever-Published-fa9c94fc-626a-446a-8b34-e6aeddcc086f
Reverse for 'blog_details' with keyword arguments ''kwargs': 'slug': 'The-Shortest-Scientific-Papers-Ever-Published-fa9c94fc-626a-446a-8b34-e6aeddcc086f'' not found. 1 pattern(s) tried: ['details/(?P<slug>[-a-zA-Z0-9_]+)$']
Request Method: POST
Request URL:    http://localhost:8000/details/The-Shortest-Scientific-Papers-Ever-Published-fa9c94fc-626a-446a-8b34-e6aeddcc086f
Django Version: 3.2.8
Exception Type: NoReverseMatch

slug有什么问题吗?

蛞蝓的models.py

class Blog(models.Model):
    slug= models.SlugField(max_length=264,unique=True)
 

请帮帮我。

【问题讨论】:

【参考方案1】:

redirect(…) [Django-doc] 不使用args=…kwargs=… 参数,它使用位置和命名参数,所以:

return redirect('blog_details', <strong>slug=slug</strong>)

注意:您可能想使用slugify(…) [Django-doc] 创建一个slug。这将生成统一的 slug,并保证这些对 &lt;slug:…&gt; 路径转换器有效。

【讨论】:

以上是关于NoReverseMatch 在 /details/The-Shortest-Scientific-Papers-Ever-Published-fa9c94fc-626a-446a-8b34-e6ae的主要内容,如果未能解决你的问题,请参考以下文章

在 url 模式中使用 include() 时出现意外的 NoReverseMatch 错误

Django:NoReverseMatch 在

NoReverseMatch 在 /details/The-Shortest-Scientific-Papers-Ever-Published-fa9c94fc-626a-446a-8b34-e6ae

NoReverseMatch at / 实现注释选项时出错

Django 测试 - NoReverseMatch

包含收藏按钮后的 NoReverseMatch