선풍기
선풍기

Reputation: 961

Django URL in template with slug

I have a template (home.html) where I want to list all the items in my model, and when I click on them, it will take the user to the details page of the item.

Upvotes: 0

Views: 1814

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599956

In your loop you call each object item. But in the URL tag you refer to instance, which does not exist. You should use item there too.

{% url 'show_menuitem' menuitem_slug=item.slug %}

Upvotes: 3

Aniket Pawar
Aniket Pawar

Reputation: 2721

Your error says keyword argument not found because you defined keyword argument named menuitem_slug in the show_menuitem url but you are passing slug as keyword argument which is not the django url patterns is looking for.

Change this line ,

 <h2><a href="{% url 'show_menuitem' slug=instance.slug %}">{{ item }}</a></h2>

to

 <h2><a href="{% url 'show_menuitem' menuitem_slug=item.slug %}">{{ item }}</a></h2>

Also make sure you change get_absolute_url method to ,

def get_absolute_url(self):
    return reverse('show_menuitem', kwargs={'menuitem_slug': self.slug})

Upvotes: 0

Related Questions