python - Django - 模板不显示 DetailView
问题描述
尝试为我的帖子列表中的每个帖子设置 DetailView 时遇到以下错误:
NoReverseMatch at /blog/post-list/ 使用关键字参数 '{'id': ''}' 反转'postdetail'。尝试了 1 种模式:['blog\/post-list/(?P\d+)/$']
这里的代码:
视图.py
def PostList(request):
postlist = Post.objects.all()
context = {
"postlist":postlist
}
template_name = "postlist.html"
return render(request,template_name,context)
def PostDetail(request,id):
post = get_object_or_404(Post,id=id)
context = {
'post':post
}
template_name = "postdetail.html"
return render(request,template_name,context)
postlist.html
{% extends "base.html" %}
{% block content %}
<div class="container">
<div class="row">
<div class="col">
{% for item in postlist %}
<ul>
<li><h3>Title:</h3> {{ item.title }} <smal>{{ item.timestamp }}</smal></li>
<li><h3>description:</h3>{{ item.description }}</li>
<li><h3>Updated:</h3>{{ item.updated }}</li>
<li><h>Author: {{ item.Author }}</h></li>
{{ item.id }}
</ul>
<a href="{% url 'blog:postdetail' id=post.id %}" class="btn btn-primary">{{ item }}</a>
{% endfor %}
</div>
</div>
</div>
{% endblock %}
网址.py
urlpatterns = [
re_path(r'^post-list/$',views.PostList,name ='postlist'),
re_path(r'^post-list/(?P<id>\d+)/$',views.PostDetail,name="postdetail"),
path ('post-create',views.CreatPost, name='post-create'),
re_path (r'^post-list/update/(?P<id>\d+)/$',views.PostUpdateis,name='update-post'),
# re_path(r'^(?P<slug_post>[-\w])+/$',views.PostDetail,name="postdetail"),
]
有谁知道如何解决这个问题?
解决方案
推荐阅读
- angular - Angular6中的测试路线
- javascript - 将超链接应用于文本的脚本
- apache-spark - 在 Spark 中,为什么我们可以广播 DataFrame 而不能广播 RDD?我们如何使用广播的 DataFrame?
- python - 检查图像 URL 是否导致 Python 中的真实图像
- c# - 如何在 UITableView 中交替行颜色
- php - Woocommerce 中 XML 导入文件的产品价格翻倍
- java - Java 错误帮助
- javascript - Sinon 存根错误:“尝试包装已经包装的帖子”,即使我正在恢复
- java - 为什么我必须添加与父 pom 的依赖项?
- database - Firebase 数据库和 Ionic