我试图使用django detailview类查看博客文章中的内容,但是当我运行本地服务器并单击博客文章中的标题链接时,当url栏更改为读取正确的文章时,实际页面保持不变,因此我无法查看单个博客文章。
这段代码是问题所在。
#view.py
class PostListView(ListView):
model = Post
template_name = 'post_list.html'
class PostDetailView(ListView):
model = Post
template = 'post_detail.html'
#urls.py
urlpatterns = [
path('post/<int:pk>', PostDetailView.as_view(), name='post_detail'),
path('', PostListView.as_view(), name='post_list'),]
#post_list.html
{% for post in post_list %}
<h2><a href="{% url 'post_detail' post.pk %}">{{ post.title }}</a></h2>
<p> {{ post.tagline }} </p>