【问题标题】:send value from anchor tag to views将值从锚标记发送到视图
【发布时间】:2019-10-12 04:20:52
【问题描述】:

我的项目中有博客字段。我想将帖子 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()

【问题讨论】:

标签: django django-models


【解决方案1】:

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

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

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

【讨论】:

  • 感谢您的回复.. 而不是 queryset 写什么
  • 你能为此写代码吗.. template_name= ''prodcutSearch/post_detail.html'
  • 我不知道如何使用 template_name 编写代码..我只想要博客的 id..所以我可以用它来评论
  • 如果我的解决方案对您有用,请接受它作为您回答的解决方案:)
猜你喜欢
  • 2018-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
相关资源
最近更新 更多