【问题标题】: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 时,会出现此错误。请帮助我。非常感谢您的帮助。

【问题讨论】:

    标签: html django django-views django-templates


    【解决方案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。

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

      &lt;a href="{% url 'mains:detail_view' <b>topic.post_owner.id</b> %}"&gt;

      &lt;a href="{% url 'mains:detail_view' <b>id=topic.id</b> %}"&gt;

      【讨论】:

      • 您好,先生,新的错误被称为“detail_view”的反向错误,未找到参数“(”,)“。尝试了 1 种模式:['(?P[^/]+)$']。请大佬帮忙
      • @Progam 更新了答案。我希望现在它应该可以工作了
      猜你喜欢
      • 2021-02-26
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 2011-09-26
      • 2019-07-21
      • 2012-05-10
      • 2013-03-02
      • 2021-12-10
      相关资源
      最近更新 更多