Home > Back-end >  Django Template js static files fail to load
Django Template js static files fail to load

Time:09-16

Its been a very long time since I had to use Django templates but I am having some trouble loading static js files. My structure is as follows:

Project > static > js > main.js

in the template I have <script type="text/javascript" src="{% static 'js/main.js' %}"></script>

I do have {% load static %} at the top of the html file and also here are my settings

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

Can someone please enlighten me as to what am I missing?

CodePudding user response:

In fact, don't think about it too complicated, as long as the routing is correct, it is the same as writing the general front-end mode.

settings.py

BASE_DIR = Path(__file__).parent
CORE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(CORE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(CORE_DIR, 'core/static'),
)

scripts.html

    <script src="/static/assets/js/vendor-all.min.js"></script>
    <script src="/static/assets/plugins/bootstrap/js/bootstrap.min.js"></script>
    <script src="/static/assets/js/pcoded.min.js"></script>

then my js real location is /core/static/assets/js/xxxx

CodePudding user response:

Replace:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')    

With:

STATICFILES_DIRS =(
        os.path.join(BASE_DIR,'static'),
    )

Then it will work fine.

  • Related