milan milan - 28 days ago 6
Ajax Question

$.ajax({}) is not a function in django

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)

Answer Source

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