generic views: object_list how to pass request variable

孤街醉人 提交于 2020-01-05 18:58:50

问题


How to pass request variable in generic views to a queryset.

For example i need to pass req_brand_slug from request to a filter in queryset:

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

回答1:


You'll have to create your own view which calls the generic view with custom params.

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)


来源:https://stackoverflow.com/questions/5141673/generic-views-object-list-how-to-pass-request-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!