简体   繁体   English

带有 AJAX 请求的 Django Like 按钮

[英]Django Like Button with AJAX Request

I'm trying to create an AJAX request in Django to update a Like Button.我正在尝试在 Django 中创建一个 AJAX 请求来更新一个 Like 按钮。

I'm getting an Error from the AJAX call.我从 AJAX 调用中收到错误。 Not sure how to troubleshoot it.不知道如何解决它。

Can anybody point me in the right direction?有人能指出我正确的方向吗?

在此处输入图像描述

.script 。脚本

$(".vote-form").submit(function(e) {
          e.preventDefault();
          let poopfact_id = $(this).attr("id");
          const voteCount = $(`.voteCount${poopfact_id}`).text();
          let url = $(this).attr('action');
  $.ajax({
            type: 'POST',
            url: url,
            data: {
              'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val(),
              'poopfact_id': poopfact_id,
            },
            
            success: function(response) {
              document.getElementById("vote_count").innerHtml = response['total_votes']
              console.log('success', response)
              
            },
            error: function(response) {
              console.log('error', response),
            },
          });

.views .views

def vote(request, poopfact_id):
    user = request.user
    post = get_object_or_404(PoopFact, id=poopfact_id)
    if request.method=="POST":
        if user.is_authenticated:
            if "upvote" in request.POST:
                if user in post.upvote.all():
                    post.upvote.remove(request.user)
                    total_votes = post.upvote.count() - post.downvote.count()
                    data = {"total_votes": total_votes}
                    return  JsonResponse(data, safe=False)
                else:
                    post.upvote.add(request.user)
                    post.downvote.remove(request.user)
                    total_votes = post.upvote.count() - post.downvote.count()
                    data = {"total_votes": total_votes}
                    return  JsonResponse(data, safe=False)
        else:
            return HttpResponseRedirect(reverse('user_login'))

.html .html

<form action="{% url 'vote' poopfact.id %}" style="display: inline-block" method="POST" class="vote-form" id="{{poopfact.id}}">
  ... 
  <div class="voteCount{{poopfact.id}}" id="vote_count">{{   poopfact.total_votes }}</div>
  <button type="submit" name="upvote" class="btn btn-primary"><i   class="fa-solid fa-up-long"></i></i></button>
  ...

Here is an alternative solution:这是一个替代解决方案:

template body:模板主体:

{% for fact in poopfacts %}
    <div id="{{ fact.id }}">{{ fact.total_votes}}</div>
    <button value="" onclick="addVote('{{ fact.id }}', 1)">+</button>
    <button value="" onclick="addVote('{{ fact.id }}', -1)">-</button>
    <hr>
{% endfor %}

.script: 。脚本:

<script>
    function addVote(id, vote) {
        let url = "{% url 'core:poop-facts-vote' %}";
        $.ajax({
            type: 'POST',
            url: url,
            data: {
            csrfmiddlewaretoken: '{{ csrf_token }}',
            'id': id,
            'vote': vote,
            },
            success: function(response) {
                total_votes = response['votes'];
                poopfact_id = response['id'];
                e = document.getElementById(poopfact_id)
                e.innerHTML = total_votes;
                console.log('success', response);
            },
            error: function(response) {
                console.log('success', response);
            },
        });
    }
</script>

views.py:意见.py:

from django.http import JsonResponse
from core.models import PoopFact
from django.shortcuts import get_object_or_404

def poop_facts_vote(request):
    id = request.POST.get('id')
    vote = int(request.POST.get('vote'))

    if request.user.is_authenticated:
        poopfact = get_object_or_404(PoopFact, id=id)
        poopfact.total_votes += vote
        poopfact.save()
        poopfact.refresh_from_db()
        return JsonResponse({'votes': poopfact.total_votes, 'id': poopfact.id})

def poop_facts(request):
    poopfacts = PoopFact.objects.all()
    return render(request, 'poop_facts.html', {'poopfacts': poopfacts})

models.py模型.py

class PoopFact(models.Model):
    total_votes = models.IntegerField()

Adapting to you upvote / downvote model:适应你的upvote / downvote模型:

def poop_facts_vote(request):
    id = request.POST.get('id')
    vote = int(request.POST.get('vote'))

    if request.user.is_authenticated:
        poopfact = get_object_or_404(PoopFact, id=id)
        if vote > 0:
            # upvote
            ...
        else:
            # downvote
            ...

        return JsonResponse({'votes': poopfact.total_votes, 'id': poopfact.id})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM