如何解决未找到带有参数 '('chempion',)' 的 'post_detail' 反转尝试了 1 个模式:['(?P<category_slug>
未找到参数为 '('chempion',)' 的 'post_detail' 反转。尝试了 1 个模式:['(?P
view.py
def post_detail(request,category_slug,slug):
post = get_object_or_404(Post,slug=slug)
try:
next_post = post.get_next_by_date_added()
except Post.DoesNotExist:
next_post = None
try:
prevIoUs_post = post.get_prevIoUs_by_date_added()
except Post.DoesNotExist:
prevIoUs_post = None
context = {
'post': post,'next_post': next_post,'prevIoUs_post': prevIoUs_post
}
return render(request,'post_detail.html',context)
urls.py
path('<slug:category_slug>/<slug:slug>/',post_detail,name='post_detail'),path('<slug:slug>/',category_detail,name='category_detail'),
post detail.html
{% if next_post %}
<a href="{% url 'post_detail' next_post.slug %}">Next</a>
{% else %}
This is the last post!
{% endif %}
解决方法
post_detail
视图需要两个 slug:一个用于类别,另一个用于帖子。
例如,如果您的 Post
模型具有 ForeignKey
到 Category
模型,您可以通过以下方式引用:
<a href="{% url 'post_detail' next_post.category.slug next_post.slug %}">Next</a>
在您看来,您可能想要检查类别和帖子的 slug,因此:
def post_detail(request,category_slug,slug):
post = get_object_or_404(Post,category__slug=category_slug,slug=slug)
# …
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。