I am Trying to implement like buttons in my Html using Django, jquery, and Ajax. I thought it was working well, but later realized that the likes are only getting incremented for the first post. Here is the HTML page
<div class="col-sm-10 col-sm-offset-1">
{% for it in lists %}
<div class="row">
<div class="col-sm-12">
<div class="thumbnail">
{% if lt.image%}
<!img src="{{ lt.image.url }}" class="img-responsive">
{% endif %}
<img src="{{ lt.image.url }}" alt="...">
<div class="caption">
<h3><a href="{% url 'posts:detail' lt.id %}">{{ lt.title}}</a><small> {{ lt.timestamp|timesince }} ago</small></h3>
<p>{{ lt.content|linebreaks|truncatechars:120 }}</p>
<p><a href="{% url 'posts:detail' lt.id %}" class="btn btn-primary" role="button">View</a></p>
<button id="likes" data-catid="{{ lt.id }}" class="btn btn-primary glyphicon glyphicon-thumbs-up" type="button">
Like {% if lt.likes > 0 %} {{lt.likes}} {% endif %}
</button>
</div>
</div>
</div>
{% endfor %}
</div>
And this is the Jquery
$(document).ready(function(){
$('#likes').click(function(event){
alert("button")
event.preventDefault();
var element=$(this);
$.ajax({
url : '/posts/like_category/',
type : 'GET',
data : { category_id: element.attr("data-catid")},
success : function(response){
element.html(' ' + response)
}
});
});
});
The likes are Incremented only for the first post, I Think the problem is with my HTML page. Please help me to sort out this problem.