帖子匹配查询不存在
Posted
技术标签:
【中文标题】帖子匹配查询不存在【英文标题】:Post matching query does not exist 【发布时间】:2021-03-14 02:18:41 【问题描述】:urls.py
path('posts/',views.posts,name='posts'),
path('<id>',views.detail_view,name='detail_view'),
views.py
def posts(request):
posts = Post.objects.all().order_by('-date_added')
context = 'posts':posts
return render(request, 'mains/post.html', context)
post.html
% block content %
% for topic in posts %
<a> topic.description</a> <a href="% url 'mains:detail_view'
topic.post_owner.id %">Show
More</a><br>
<br>
% empty %
No yet.</li>
% endfor %
。当我单击帖子页面中的 show_more 时,会出现此错误。请帮助我。非常感谢您的帮助。
【问题讨论】:
【参考方案1】:你应该使用这个
<a href="% url 'mains:detail_view' topic.id %">
而不是
<a href="% url 'mains:detail_view' topic.post_owner.id %">
因为您在 views.py 中传递 pk 而不是在您的模板中
【讨论】:
【参考方案2】:您正在使用帖子所有者的 PK 创建超链接,但是,您的视图期待帖子的 PK。
因此,将模板中的超链接逻辑更新为,
<a href="% url 'mains:detail_view' <b>topic.post_owner.id</b> %">
到
<a href="% url 'mains:detail_view' <b>id=topic.id</b> %">
【讨论】:
您好,先生,新的错误被称为“detail_view”的反向错误,未找到参数“(”,)“。尝试了 1 种模式:['(?P以上是关于帖子匹配查询不存在的主要内容,如果未能解决你的问题,请参考以下文章