首页 > 解决方案 > 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')

标签: djangofilter

解决方案


推荐阅读