How to set up Django website with jQuery

Viewed 59048

I don't know why, but I couldn't figure out how to activate jQuery on my website. All sites doing tutorials 'Starting jQuery on Django' started with JQuery already working on their site. Anyway, instead of downloading it and putting it in my folder somewhere, I decided to go the cheat-way and use Google's. Therefore, in my base page, I had the following piece of code

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js">
</script>

And everything worked great, until I had to add some libraries. How do I set up my own jQuery so that it works with my templates?

4 Answers

I put my jquery file in the same place as SaiyanGirl.

In my setting I had this by default in INSTALLED_APPS: 'django.contrib.staticfiles',

At the top of my html page I have this:

{% load static %}
<!DOCTYPE html> 

I have this in my head tag:
<script type="text/javascript" src="{% static 'jquery-3.5.0.js' %}"></script>

This is how I tested it (in the head tag too):

<script type="text/javascript"> 
window.onload = function() {
   if (window.jQuery) {
      alert('jQuery is loaded');
   } else {
      alert('jQuery is not loaded');
</script>
Related