javascriptdjangodjango-staticfilesdjango-static

how to import functions inside /static/ dir Django


When i make script for my django project i made a few functions but when i started optimising it and divide for few files i encounter a problem with the importing of function

cart_methods.js

export function cartFunction() {
    console.log("work");
}

home_script.js

import { cartFunction } from './cart_methods.js';
cartFunction();

with this part of code my .js not work at all not even small part of code not working. it stop everything

every files is in same folder

static> 
       shop>
             assets>
                     js>
                         cart_methods.js
                         home_script.js
    

 

Solution

  • Make sure you are adding type='module' to your html script tag and using correct path.

    Try out below code:

      <script type="module" src="{% static 'shop/assets/js/home_script.js' %}"></script>