【问题标题】:generic views: object_list how to pass request variable通用视图:object_list 如何传递请求变量
【发布时间】:2011-07-05 17:29:00
【问题描述】:

如何将通用视图中的请求变量传递给查询集。

例如,我需要将 req_brand_slug 从请求传递到查询集中的过滤器:

all_by_brand = {
    'queryset': Br.objects.filter(slug=req_brand_slug)
}
url(r'^model/(?P<req_brand_slug>[\w|-]+)/$', all_by_brand , name='brand'), 

【问题讨论】:

    标签: django django-generic-views


    【解决方案1】:

    您必须创建自己的视图,该视图使用自定义参数调用通用视图。

    from django.views.generic.list_detail import object_list
    
    def my_view(request, req_brand_slug):
        extra_context = {}
        return object_list(request, queryset=Br.objects.filter(slug=req_brand_slug),
                           template_name="my_template.html",
                           paginate_by=20,
                           extra_context=extra_context)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 2020-08-26
      • 1970-01-01
      • 2017-05-27
      相关资源
      最近更新 更多