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...

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

1 Comment

I needed also to add the request parameter, this way: return object_list(request, queryset=queryset)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.