html - 帖子匹配查询不存在
问题描述
网址.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 时,会出现此错误。请帮助我。我将非常感谢您的帮助。
解决方案
您正在使用帖子所有者的 PK创建超链接,但是,您的视图期待帖子的 PK。
因此,将模板中的超链接逻辑更新为,
<a href="{% url 'mains:detail_view' topic.post_owner.id %}">
到
<a href="{% url 'mains:detail_view' id=topic.id %}">
推荐阅读
- ios - 按下 UITableView 行时正确触发视图控制器打开的问题?
- google-bigquery - BigQuery 架构更新,同时从其他表复制数据
- android - JAVA code visible in android APK - APK Analyzer
- angular - 如何延迟加载子路由?
- javascript - 如何在 Rhino 中运行 ES6 和 npm deps
- java - hasItems 带有可迭代包含的断言错误
- kubernetes - Azure Kubernetes NGINX 入口重定向到 www 停止工作
- ruby-on-rails - Rails API忘记密码未呈现视图
- search - 如何使用 nominatim.openstreetmap.org/search 将结果限制在 1 公里半径范围内?
- apache-spark - Apache Zeppelin 笔记本部署