title: django like buttno using ajax
body: <p>i newbie with django , I trying to make a like button with ajax, but it's not working, ajax is not sending the data to the server and I don't know why, any solution Django folks ?</p>
  
  <p>product/url.py : </p>
  
  <pre><code> path('upvote/',views.upvote,name='upvote'),
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>@login_required(login_url='/accounts/signup')
  def upvote(request):
      if request.method == 'POST':
          ans_id = request.GET['product_id']
      if ans_id:
          product = get_object_or_404(Product,pk=ans_id)
          if product:
            product.votes_total+=1
            product.save()
          # return redirect('/products/' + str(product_id))
            return render(request, 'products/home.html', {'likes': product.votes_total})
  
  </code></pre>
  
  <p>template/html </p>
  
  <pre><code>{% extends 'base.html' %}
  
  {% block content%}
  {% for product in product.all%}
  &lt;div class="container"&gt;
  &lt;div class="row pt-3"&gt;
      &lt;div class="col-2" onclick="window.location='{% url 'detail' product.id%}';" style="cursor : pointer;"&gt;
       &lt;img src="{{product.image.url}}" class="img-fluid " alt=""&gt;
      &lt;/div&gt;
      &lt;div class="col-6 " &gt;
          &lt;h1 class="textcolor"&gt;{{ product.title}}&lt;/h1&gt;
          &lt;p&gt;{{product.summary}}&lt;/p&gt;
      &lt;/div&gt;
      &lt;div class="col-4"&gt;
          &lt;b id="like_count"&gt;{{ product.votes_total }}&lt;/b&gt;
          &lt;a  data-catid="{{ product_id}}"&gt; &lt;button class="btn btn-warning btn-dark  likes-button"  id="likes" &lt;span class="oi oi-thumb-up" id="like"&gt;&lt;/span&gt; Upvote {{product.votes_total}}&lt;/button&gt;&lt;/a&gt;
      &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  
  {% endfor%}
  
  {% endblock%}
  
  </code></pre>
  
  <p>ajax : </p>
  
  <pre><code> $('.likes-button').click(function(){
        $.ajax({
                 type: "POST",
                 url: '/ajax/upvote/',
                 data: {'slug': $(this).attr('name'), 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                 dataType: "json",
                 success: function(response) {
                        alert(response.message);
                        alert('Company likes count is now ' + response.likes_count);
                  },
                  error: function(rs, e) {
                         alert(rs.responseText);
                  }
            });
      })
  
  </code></pre>