首页 > 解决方案 > 将值从锚标记发送到视图

问题描述

我的项目中有博客字段。我想将帖子 ID 发送到 views.py 告诉我我做错了什么

在 blog.html 中

<a href="{% url 'post-detail' post.id %}" class="btn btn-primary">Read 
more</a>

在网址中:

path('post_detail/<int:pk>/', PostDetailView.as_view(), name='post- 
detail')

在意见中:

class PostDetailView(DetailView):
model = Post

def get_queryset(self):
    queryset = super().get_queryset()
    search = self.request.GET.get('pk')
    if search:
        queryset = Post.objects.filter(page_id=search)
        print(queryset)

    else:
        return queryset.none()

标签: djangodjango-models

解决方案


添加模板属性,它将起作用。

template_name = 'app-name-template/single_post.html'

也查询它你的情况不需要所以删除它。


推荐阅读