$.ajax({})不是djang中的函数

2024-09-30 08:34:12 发布

您现在位置:Python中文网/ 问答频道 /正文

这种相关的问题我见过很多,但没有一个能解决我的问题。还有其他原因为什么这不起作用,但我觉得很难解决这个问题。这是密码

在基本.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 %}

包括/js.html文件在

^{pr2}$

在主页.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 %}

我已经检查jquery是否已加载,并且代码是否运行到if(likeUrl)


Tags: divdataifvarhtmljsfunctionthis
1条回答
网友
1楼 · 发布于 2024-09-30 08:34:12

在您可以阅读的描述中使用jqueryslim

/*! 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在slim版本中被排除在外

更多release-note

相关问题 更多 >

    热门问题