django - 将值从锚标记发送到视图
问题描述
我的项目中有博客字段。我想将帖子 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()
解决方案
添加模板属性,它将起作用。
template_name = 'app-name-template/single_post.html'
也查询它你的情况不需要所以删除它。
推荐阅读
- azure - 从 Azure 数据工厂 (ADF) 调用 Azure Function 时出现异常
- mysql - 如何在一张表 MySQL 中制作故事和保存的故事?
- python - 矩阵及其逆的乘积
- email - JavaMail 与 Outlook
- sql - SQL Count 然后 Sum?
- java - 如何将元素添加到java中排序数组中的正确位置
- mysql - 将连接转换为mysql上的子查询
- ruby - 为已获救的异常编写 RSpec 测试
- apache-spark - 如何在第二个数据集上过滤内部数组 ID?
- javascript - 在 Node.js 中将业务逻辑从控制器重构为服务