Я использую django в серверной части
вот мое мнение в django:
Код: Выделить всё
def favourite(request, post_id):
user = request.user
post = Post.objects.get(id=post_id)
profile = Profile.objects.get(user=user)
is_favourite = False
if profile.favourite.filter(id=post_id).exists():
profile.favourite.remove(post)
is_favourite = False
else:
profile.favourite.add(post)
is_favourite = True
response = {
"is_favourite": is_favourite,
}
return JsonResponse(response)
Код: Выделить всё
$(document).ready(function () {
$('.favourite-button').click(function (e) {
e.preventDefault();
var button = $(this);
var post_id = button.data('id');
var url = button.attr('href');
$.ajax({
type: 'POST',
url: url,
data: {
'csrfmiddlewaretoken': '{{ csrf_token }}',
},
success: function (response) {
if (response.is_favourite) {
button.find('i').addClass('liked');
} else {
button.find('i').removeClass('liked');
}
},
error: function (response) {
console.error('Error:', response);
}
});
});
});Код: Выделить всё
.action-buttons i.liked{
color: var(--btn-color);
}Код: Выделить всё
[url={% url ]
[i][/i]
[/url]
[i][/i]
[i][/i]
[url={% url ]
[i][/i]
[/url]
Подробнее здесь: https://stackoverflow.com/questions/787 ... shing-page
Мобильная версия