1

I have a url set up dynamically for a project using:

path('project/<int:pk>', ProjectView.as_view(), name='project')

How can I make this so I can use two parameters, something like this:

path('project/<int:pk>/<int:category', ProjectView.as_view(), name='project')

So I need to set up links to each category, so the user will see only the updates from Project A category 1 of 7.

1
  • Exactly the same way, but you forgot a closing bracket >. Commented Jul 23, 2019 at 14:16

1 Answer 1

1

If ProductView is a DetailView, you need to alter the get_queryset a bit, like:

from django.views.generic.detail import DetailView

class ProductView(DetailView):
    model = Product
    template = 'some_template.html'

    def get_queryset(self):
        return super().get_queryset().filter(
            category__id=self.kwargs['category']
        )

Here we thus will filter the queryset first by 'category', and the boilerplate code of the DetailView will then filter by the primary key pk.

In your templates, you can generate urls, with:

{% url 'project' pk=some_pk category=some_category_id %}

or if you for example redirect:

     return redirect('project', pk=some_pk, category=some_category_id)
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm, this gives me the following error: "get_queryset() takes 0 positional arguments but 1 was given" what am I passing that I wasn't before?
@iFucntion: sorry I forgot the self.
Thank you, I have realized I have a bigger problem, I was trying to use data from two different models, so unfortunately, again, no joy. PK refers to the Project model and category is a foreign key to Update model from another table.

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.