首页 > 解决方案 > 帖子匹配查询不存在

问题描述

网址.py

path('posts/',views.posts,name='posts'),

path('<id>',views.detail_view,name='detail_view'),

视图.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 时,会出现此错误。请帮助我。我将非常感谢您的帮助。

标签: htmldjangodjango-viewsdjango-templates

解决方案


您正在使用帖子所有者的 PK创建超链接,但是,您的视图期待帖子的 PK。

因此,将模板中的超链接逻辑更新为,

<a href="{% url 'mains:detail_view' topic.post_owner.id %}">

<a href="{% url 'mains:detail_view' id=topic.id %}">

推荐阅读