[Answered ]-Django & Ajax – "Like" button, how to make it work for mutliple posts on a page?

1👍

You need to use the value of the clicked button, so:

data: {
    newsid: $(this).val(),
    csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val(),
    action: 'post'
},

Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.


Note: You do not have to store the number of items of a ManyToManyField in another field. You can use .annotate(…) [Django-doc] when you need to determine this by the database. Storing this explicitly in a field is a form of data duplication, and it turns out that keeping these in sync is harder than what one might expect.

Leave a comment