通用视图:object_list如何传递请求变量 [英] generic views: object_list how to pass request variable
本文介绍了通用视图:object_list如何传递请求变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将通用视图中的请求变量传递给查询集.
How to pass request variable in generic views to a queryset.
例如,我需要将请求中的 req_brand_slug 传递给查询集中的过滤器:
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'),
推荐答案
您必须创建自己的视图,该视图使用自定义参数调用通用视图.
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)
这篇关于通用视图:object_list如何传递请求变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文