Newbie
Newbie

Reputation: 53

How to get clicked item in view

I am a relative beginner in django. In one of my templates, I am creating a list of items stored in a database. With the following template.html code:

<ul>
    {% for item in items %}
       <li>
          {{ item.name }}
       </li>
    {% endfor %}
</ul>

Rendering from views.py code:

def category_view(request):
    list = Category.objects.order_by('name')
    context = {'items': list}
    return render(request, "template.html", context=context)

Now, I want to get the name of the list item (category) on which the user has clicked, so that I can show user sub-categories on the next page depending on what was selected on the previous page.

Kindly share ideas of how to get the clicked item and flow to send dependent list to the next page.

Upvotes: 0

Views: 420

Answers (2)

M Burak
M Burak

Reputation: 109

You can redirect the user by clicking to item's subcategory_view. So, your list may have links with item's id (or slug). Like that:

<ul>
    {% for item in items %}
       <li>
          <a href="/items/{{item.id}}">{{ item.name }}</a>
       </li>
    {% endfor %}
</ul>

Upvotes: 0

Vit Amin
Vit Amin

Reputation: 683

{{ item.name }} should be a link to a subcategory_view's path. So user will be redirected to your subcategory page.

Upvotes: 2

Related Questions