简体   繁体   中英

Django static files not working for other templates except index

I am developing an agency web app with django templates & static files. But I am having trouble with loading static files on pages other than index . Here's my folder heirarchy. Again, I am having no problems loading static files on my index page. Here's code of my additional page services.html (one I am trying to load static files on):

{% extends 'base.html' %}
{% load static %}
{% block content %}
  ...
  ...
  ...
{% endblock %}

My base.html is working totally fine with the homepage index.html . I've included this on my settings.py

STATIC_URL = '/static/'
STATIC_ROOT = '/static/'


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

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

And, my views.py and urls.py files are working fine too.

Also my CSS file is correctly linked to the services.html file. I can tell this from the inspection. Here's the error list shown on inspection. Help will be appreciated. Thank you.

Your static files are stored in the core folder, so one way is to change the settings.py to :

STATIC_URL = '/static/'
STATIC_ROOT = '/static/'


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

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

The other way is to use python manage.py collectstatic . A new staticfiles directory will be created which has folders for admin (the built-in admin has its own static files), staticfiles.json , and whatever directories are in your static folder.

RESOLVED.
I was apparently not using Django HTML tags to link my static CSS/JS files.
So, I initialized static files on my base.html with {% load static %} and replaced links with href="{% static 'assets/css/style.css' %} format. This worked out. Remember that full path shouldn't be written ie {% static './static/assets/...' %} instead just use {% static 'assets/...' %} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM