Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 2.0 url parameters in get_queryset

I would like to filter subcategories based on the category id from the URL

For a constant value, it works without a problem

return Subcategory.objects.filter(category = 1)

views.py

class SubcategoriesListView(ListView):
    model = Subcategory
    template_name = 'app/categories/index.html'
    def get_queryset(self):
        return Subcategory.objects.filter(category = category_id)

urls.py

path('categories/<int:category_id>/', app.views.SubcategoriesListView.as_view(), name='subcategories'),

models.py

class Subcategory(models.Model):
   title = models.CharField(max_length=30)
   category = models.ForeignKey(Category, on_delete=models.CASCADE)

Traceback

NameError at /categories/1/
name 'category_id' is not defined

views.py in get_queryset
return Subcategory.objects.filter(category = category_id) 
like image 773
KamWebDev Avatar asked Aug 08 '18 21:08

KamWebDev


People also ask

How to get a parameter from a URL in Django?

To get a parameter from the URL, you have to perform the steps explained below: Create and map a path to a view in the application’s URLs file and pass the parameters to the view Define a function in the view that will take the parameter and pass the parameters to Django template.

How to pass a UUID as a parameter in Django?

In this URL, python-django-get is the slug parameter. uuid: A formatted UUID. You can use this path converter if you want to pass a UUID as the parameter. An example of a UUID is: 123e4567-e89b-12d3-a456-426614174023. path: This path converter consists of a non-empty string, including the “/” symbol, and is defined to pass a path as the parameter.

How to pass a URL parameter from the URL to view?

You can pass a URL parameter from the URL to a view using a path converter. But, firstly you have to create a path and map it to a view. For this, you have to edit your application’s urls.py file. A sample urls.py file will look like this: Then “ products ” will be the URL endpoint.

What is viewpara function in Django?

The views.viewpara is the function defined in the views.py file that will be executed when the request is made. The views.py file will be: The above function will receive the parameter from the request. I have passed this parameter to a Django template named result.html as a dictionary object.


Video Answer


1 Answers

You can obtain the URI positional and named parameters in a class-based view with self.args (a tuple) and self.kwargs (a dictionary) respectively.

Here you defined the category_id as a named parameter, so you can obtain its corresponding value with self.kwargs['category_id']:

class SubcategoriesListView(ListView):
    model = Subcategory
    template_name = 'app/categories/index.html'
    def get_queryset(self):
        return Subcategory.objects.filter(category_id=self.kwargs['category_id'])

Since the id is an integer, you thus filter on category_id, not on category.

like image 140
Willem Van Onsem Avatar answered Oct 05 '22 20:10

Willem Van Onsem