django - django按参数过滤
问题描述
我正在构建的网站有 6 个配置文件(办公桌),可以通过页面顶部的按钮访问这些配置文件,使用:
path('home/<slug:slug>/', HomeView.as_view(), name='homepage')
并通过一个按钮访问:
{% url 'homepage', slug=desk.slug %}
所以在查询集中被过滤。如何访问该值以便在模板中使用它来进行样式设置?基本上我想突出显示页面上的桌面按钮,以便用户对他的位置有视觉反馈。有没有办法很好地做到这一点,因为目前我能想到的唯一方法是使用:
{% request.url %}
然后拆分网址的末尾,但这对我来说似乎是一个讨厌的黑客攻击。不幸的是,谷歌无法通过单词过滤器,所以我得到的只是 django-filter 文档和问题。
更新:添加视图:
class HomeView(ListView):
template_name = pcc_homepage/home.html'
def get_context_data(self, **kwargs):
context = super().get_context_data()
context['desks'] = Desk.objects.all()
context['updates'] = Update.objects.all().order_by('-published')
return context
def get_queryset(self):
return Handover.objects.filter(desk__slug=self.kwargs['slug']).order_by('-published')
解决方案
推荐阅读
- cookies - 使用 clj-http.cookies 在请求中设置 cookie
- swift - 手表按钮按下崩溃
- python - 简单的 Pandas df 计算在无穷大中丢失
- python - RE 在 pythex 中有效,但在 python 中无效
- javascript - 将正则表达式缩短为单个匹配组
- java - 使用 iText 7.1 从 PDF 中删除链接
- javascript - React Dropzone 组件 - 如何动态更改 postUrl?
- html - 网格区域不会移动到具有百分比值的网格行中
- python - 为什么 set.add() 重新排序元素?
- react-native - 当我创建子组件时,React Native 挂起