Reputation: 768
In Django, I want to display a detailed view of model objects by using the get_absolute_url method as described here.
The URL structure for each detail is something like this...
url.com/company/(?P< company_id >[0-9]+)/people/(?P< slug >[0-9]+)/$
where .../people/ is the list view and the .../people/slug/ is the detail for each object
Which returns the NoReverseMatch error like so
NoReverseMatch at /company/google/people/
as I put the href='{{ people.get_absolute_url }}'
in my template.
Does anybody know if using the 'company_id' kwarg in the URL is causing problems rendering the page with the get_absolute_url href for the 'slug' kwarg?
I've done this many times before with a hardcoded URL before the kwarg variable which worked; such as 'company/people/(?P< slug >[0-9]+)/$' - note there's only one keyword
Full Error:
Reverse for 'people-detail' with arguments '()' and keyword arguments '{u'slug': u'798224891221678'}' not found. 1 pattern(s) tried: ['company/(?P< company_id >[0-9]+)/people/(?P< slug >[0-9]+)/$']
get_absolute_url method:
def get_absolute_url(self):
return reverse('people-detail', kwargs={'slug': self.slug})
Upvotes: 2
Views: 3595
Reputation: 309089
If your url pattern people-detail
contains a named group company_id
, then you must include that when you try to reverse it. For example, if your model has a foreign key to the Company
model, then you might be able to do:
def get_absolute_url(self):
return reverse('people-detail', kwargs={'slug': self.slug, 'company_id': self.company_id})
Upvotes: 4