Removing element from for loop html django?
I have a for loop in my html to show the user their favourite games. Like this:
<div class="dropdown-cart-products">
{% if user.is_authenticated %}
{% for i in wishlist %}
<div class="product">
<div class="product-details">
<h4 class="product-title">
<a href="{% url 'detail' i.oyunlar.platformurl i.oyunlar.slugyap %}">{{i.oyunlar.title}}
{{i.oyunlar.platform}}</a>
</h4>
<span class="cart-product-info">
<!-- <span class="cart-product-qty">1</span>-->
Anlık en ucuz Fiyatı: {{i.oyunlar.en_ucuz}}
</span>
</div><!-- End .product-details -->
<figure class="product-image-container">
<a href="{% url 'detail' i.oyunlar.platformurl i.oyunlar.slugyap %}"
class="product-image">
<img src=" {{i.oyunlar.image}}" alt="product">
</a>
<a href="javascript:{document.getElementById('upvote1').submit()}" class="btn-remove" title="Favorilerden Çıkar"><i
class="icon-retweet"></i></a>
<form id="upvote1" method="POST" action="{% url 'favhome' i.oyunlar.game_id %}">
{% csrf_token %}
</form>
</figure>
</div><!-- End .product -->
{% endfor %}
I am trying to remove element like this:
form id="upvote1" method="POST" action="{% url 'favhome' i.oyunlar.game_id %}">
{% csrf_token %}
</form>
The problem is that I can only remove first element in this loop cannot remove the second one third one or last one. How can I change my code to remove the selected element?
You have duplicate identifiers for your html elements. They all use "upvote1"
You can used {{ forloop.counter }}
to provide an unique enumeration identifier for your elements.
<a href="javascript:{document.getElementById('upvote{{ forloop.counter }}').submit()}" class="btn-remove" title="Favorilerden Çıkar"><i class="icon-retweet"></i></a>
<form id="upvote{{ forloop.counter }}" method="POST" action="{% url 'favhome' i.oyunlar.game_id %}">
{% csrf_token %}
</form>
Note: you can use the models
id
also if this is unique. e.g. {{ i.id }}