F
F
Fedor_PV2018-08-26 17:42:00
Django
Fedor_PV, 2018-08-26 17:42:00

How to make a like button if there are a lot of posts?

Gentlemen, help a newbie and a noob, I can’t figure out what to do in such a situation: I wrote the code for the like button, everything works perfectly if the post (which is liked) is alone on the page.
jquery code:

$(function(){
$('span a').on('click', function(e){
e.preventDefault();
var url = this.href;
$.get(url, function(data){
$('#like').html('<img src="/static/countries/like.svg">' + data);
});
});
});

And here is the template in Django:
…
{% for description in descriptions %}
...
<span><a href="{% url 'countries:likes' description.id %}"><span id="like"><img src="...">&nbsp{{ description.likes.count }}</span></a></span>&nbsp men like it


The problem is obvious - if there is only one post (description), then the JQuery code gives the number of likes correctly in id = like, everything works, but if there are several posts, and they are displayed on the page all together ({% for description in descriptions %}), then each is the same , but this should not be, id must be unique. As a result, jQuery gives the number of likes only to the first post, the rest do not work. How to solve this problem? <span id="like>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question