javascriptdjangonouislider

Problem importing nouislider in Django project


In my base template i am trying to link to the nouislider library:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- existing code in the head -->
<!-- Include noUiSlider CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/15.7.1/nouislider.css" 
    integrity="sha512-MKxcSu/LDtbIYHBNAWUQwfB3iVoG9xeMCm32QV5hZ/9lFaQZJVaXfz9aFa0IZExWzCpm7OWvp9zq9gVip/nLMg==" crossorigin="anonymous" 
referrerpolicy="no-referrer" />
<!-- existing code in the head -->
</head>
<body>
<!-- existing code in body -->

<!-- Include noUiSlider library -->
   
<script src="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/15.7.1/nouislider.min.js" 
   integrity="sha512-UOJe4paV6hYWBnS0c9GnIRH8PLm2nFK22uhfAvsTIqd3uwnWsVri1OPn5fJYdLtGY3wB11LGHJ4yPU1WFJeBYQ==" 
   crossorigin="anonymous" 
   referrerpolicy="no-referrer">
</script>

    <script src="{% static 'javascript/slideshow.js' %}" defer></script>
    <script src="{% static 'javascript/scrollToTop.js' %}" defer></script>
    <script src="{% static 'javascript/productFilter.js' %}" defer></script>
    
</body>

This base template is extended in another template.

Now, the problem is that in my productFilter.js i have all the javascript code i need including the ones for the nouislider, however, while the codes in the productFilter.js are being executed as intened, the nouislider itself does not work on the page. The slider does not even show up. The odd thing is that if i, instead of using the productFilter.js, just put everything in a HTML file like this -

<body>
    <!-- existing code in body -->

<!-- Include noUiSlider library -->
   
<script src="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/15.7.1/nouislider.min.js" 
   integrity="sha512-UOJe4paV6hYWBnS0c9GnIRH8PLm2nFK22uhfAvsTIqd3uwnWsVri1OPn5fJYdLtGY3wB11LGHJ4yPU1WFJeBYQ==" 
   crossorigin="anonymous" 
   referrerpolicy="no-referrer">
</script>

<script>
// contents of the productFilter.js
</script>
</body>

the slider shows and works.

what i dont understand is why it wont work when the javascript codes are in a js file but work when those same js codes are in a html file embedded in a script tag. Everything is linked properly because the other javascript codes in the productFilter.js are working just fine.

What am I missing?


Solution

  • I solved the problem. The problem was not how I was linking the external Javascript, since, as I said already, the other Javascript codes unrelated to noUIslider were executing properly.

    The problem was that the JavaScript file - productFilter.js, could not directly access Django template variables like {{ min_price }} and {{ max_price }} and I used them for my min and max values in the noUIslider. Since the Javascript file could not retrieve the data associated with those variables, the noUIslider could not show up or work.

    I solved the problem by passing the data through data attributes and then accessing the attributes in my Javascript. in my html I have -

    <body data-min-price="{{ min_price }}" data-max-price="{{ max_price }}">
    

    in my productFilter.js I have -

    document.addEventListener("DOMContentLoaded", function() {
    // Get the minimum and maximum prices from Django template
    var minPrice = parseFloat(document.body.getAttribute('data-min-price'));
    var maxPrice = parseFloat(document.body.getAttribute('data-max-price'));
    
    // Check if the slider element already has a noUiSlider instance
    var priceSlider = document.getElementById('priceSlider');
    if (!priceSlider.classList.contains('noUi-target')) {
        // Initialize the slider only if it's not already initialized
        noUiSlider.create(priceSlider, {
            start: [minPrice, maxPrice], // Initial values for the handles
            connect: true,  // Connect the handles with a colored bar
            range: {
                'min': minPrice,
                'max': maxPrice
            }
        });
    }
    

    And now it works.