1

Such related question I have seen so many but none solved my problem. There is other reason why this is not working but I am feeling difficult to figure out the issue. Here is the code

base.html

<div class="sections-wrapper">
  {% block pagecontent %}
  {% endblock pagecontent %}
</div>
{% block js %}
  {% include 'includes/js.html' %}
  <script>
    $(document).ready(function(){
      function updateText(btn, newCount, verb){
      btn.text(newCount + " " + verb)
    }
    $(".like-company").click(function(e){
      e.preventDefault()
      var this_ = $(this)
      var likeUrl = this_.attr("data-href")
      var likeCount = parseInt(this_.attr("data-likes")) | 0
      var addLike = likeCount + 1
      var removeLike = likeCount - 1
      if (likeUrl){
      $.ajax({
        url: likeUrl,
        method: "GET",
        data: {},
        success: function(data){
          console.log(data)
          var newLikes;
          if (data.liked){
            updateText(this_, addLike, "Unlike")
          } else {
            updateText(this_, removeLike, "Like")
          }
          }, error: function(error){
              console.log(error)
              console.log("error")
            }
        })
      }
    })
    })
  </script>
{% endblock js %}

includes/js.html

{% load static %}
<script src="{% static 'js/jquery.slim.min.js'%}"></script>
<script src="{% static 'js/tether.min.js' %}"></script>
<script src="{% static 'js/bootstrap4.min.js'%}"></script>

home.html

{% extends 'base.html' %}
{% block pagecontent %}
  <div class="container">
    {% if companies %}
      <h3 class="text-center bold categories">COMPANIES</h3>
      {% for company in companies %}
        {% if forloop.first %}<div class="row">{% endif %}
              <a href="{{ company.get_like_url }}" class="btn btn-link like-company" data-href="{{ company.get_like_url }}" data-likes="{{ company.likes.count }}"><i class="fa fa-thumbs-o-up"></i>{{ company.likes.count }} like</a>
        </div>
      {% endfor %}
    {% endif %}
  </div>
{% endblock pagecontent %}

I have checked that the jquery is loaded and also the code is working up to if (likeUrl)

5
  • 1
    What's the actual error message? (Verbatim.) Commented Sep 21, 2017 at 13:35
  • in base {% include 'includes/js.html' %} js script in includes/nav.html you have typo in question or in project? Commented Sep 21, 2017 at 13:36
  • sorry it is typo in the question. I will fix it Commented Sep 21, 2017 at 13:41
  • I get error message as $.ajax({}) is not a function Commented Sep 21, 2017 at 13:42
  • Have you tried replacing $ with jQuery (case sensitive)? Commented Sep 21, 2017 at 13:45

2 Answers 2

3

you use jquery slim in description you can read

/*! jQuery v3.2.1 -ajax,-ajax/jsonp,-ajax/load,-ajax/parseXML,-ajax/script,-ajax/var/location,-ajax/var/nonce,-ajax/var/rquery,-ajax/xhr,-manipulation/_evalUrl,-event/ajax,-effects,-effects/Tween,-effects/animatedSelector | (c) JS Foundation and other contributors | jquery.org/license */

ajax is exclude in slim version

more release-note

Sign up to request clarification or add additional context in comments.

2 Comments

Or link self.valuable_content: stackoverflow.com/a/37750393/1600649
yeah it worked now when using minified version instead of slim
0

Yes, include ajax in slim version or use min library instead of jquery.slim.min.js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.