tags:

views:

128

answers:

1

I would like to make a queryset on a generic view in this way:

 category_info = {
      'queryset' : ModelObject.objects.filter(category=category_id)
 }

where the category_id will be stated on the URL

 (r'^category/(?P<category_id>\d+)$', 'object_list', category_info )

But I don't know how to take the data from the URL and pass it to the category info...

+2  A: 

You'll have to define your own view and return the generic view from within:

urls.py:

(r'^category/(?P<category_id>\d+)$', 'myapp.views.category_list')

myapp/views.py

from django.views.generic.list_detail import object_list
def category_list(request, category_id):
    queryset = ModelObject.objects.filter(category=category_id)
    return object_list(request, queryset=queryset)

You can also customise the generic view further, using the parameters mentioned in the documentation. (You may like to also verify that the category exists, throwing a 404 when it doesn't)

Will Hardy
I needed also to add the `request` parameter, this way:`return object_list(request, queryset=queryset)`
Khelben
Whoops, updated.
Will Hardy