Sahin Urkin
Sahin Urkin

Reputation: 279

How to pass params dynamicaly from template to ListView in Django?

I want to pass params dynamically to ListView via URL:

views.py:

from django.shortcuts import render
from django.views import generic
from objects.models import Object


class UserObjectsView(generic.ListView):
    template_name = 'user_objects.html'

    def get_queryset(self):
        return Object.objects.all()

urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('user/<int:pk>/', views.UserObjectsView.as_view(), name='user-objects')
]

template where I call this url:

<h3><a href="{% url 'user-objects' %}{{ user.id }}">Objects</a></h3>

I want to pass this user.id dynamically, but right now it appers error:

Reverse for 'user-objects' with no arguments not found. 1 pattern(s) tried: ['objects/user/(?P[0-9]+)/$']

Upvotes: 2

Views: 489

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476740

You pass the parameters in the template, like:

<h3><a href="{% url 'user-objects' pk=user.id %}">Objects</a></h3>

Upvotes: 2

Related Questions