问题
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