Home > Blockchain >  Its necessary to start an app in Django to load javaScript files?
Its necessary to start an app in Django to load javaScript files?

Time:12-21

i'm making a portfolio on Django Python, and i have successfully loaded all the assets/templates from my project, (such as .css, .jpg, .png files) but the javascript files '.js' are not working.

i saw several questions about this issue with Javascript, and i tried all of the solutions that i saw.

  1. i have the {% load static %} putted in the beginning of my index.html file
  2. i also putted the <script src="{% static 'assets/js/main.js' %}"></script> before each call to the files
  3. my static settings are the following:
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'portfolio/static/')
]
  1. everything seems to be OK in the settings.py file , and the website can load all the images, css, etc.. but as i mentioned before, the javascript seems to not loading.
  2. i don't have an app created in my Django project, the dir tree is the following:
├── /Desktop/portfolio
├── manage.py
├── portfolio
│   ├── asgi.py
│   ├── __init__.py
│   ├── settings.py
│   ├── static
│   │   ├── assets
│   │   │   ├── css
│   │   │   │   ├── files....
│   │   │   ├── js
│   │   │   │   ├── breakpoints.min.js
│   │   │   │   ├── browser.min.js
│   │   │   │   ├── jquery.min.js
│   │   │   │   ├── jquery.scrollex.min.js
│   │   │   │   ├── jquery.scrolly.min.js
│   │   │   │   ├── main.js
│   │   │   │   └── util.js
│   │   │   ├── sass
│   │   │   │   ├── libs
│   │   │   │   │   ├── scss files ..
│   │   │   └── webfonts
│   │   │       ├── webfonts files..
│   │   └── images
│   │       ├── .jpg files ...
│   ├── templates
│   │   └── index.html
│   ├── urls.py
│   ├── views.py
│   └── wsgi.py
├── Procfile
├── README.md
├── requirements.txt
└── static
   ├── admin 
   ├── assets
   │   ├── css
   │   │   ├── fontawesome-all.min.css
   │   │   ├── images
   │   │   │   ├── ie
   │   │   │   │   └── grad0-15.svg
   │   │   │   └── overlay.png
   │   │   └── main.css
   │   ├── js
   │   │   ├── breakpoints.min.js
   │   │   ├── browser.min.js
   │   │   ├── jquery.min.js
   │   │   ├── jquery.scrollex.min.js
   │   │   ├── jquery.scrolly.min.js
   │   │   ├── main.js
   │   │   └── util.js
   │   ├── sass
   │   │   ├── libs
   │   │   │   ├── libs files
   │   └── webfonts
   │       ├── webfonts files 
   └── images
       ├── .jpg files

Anyone could help me? Thanks a lot

CodePudding user response:

If you use module in JS file you can try to put type="module" in script tag.

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

CodePudding user response:

Your directory structure does not seem right. You do need an app. What you've created so far is a project, and their is a difference as noted in What’s the difference between a project and an app in Django world?.

  • Related