Home > OS >  my static folder where I place my js ans css files is not at the root of my django project. How to c
my static folder where I place my js ans css files is not at the root of my django project. How to c

Time:12-08

Here is the configuration of my static_root variable: STATIC_ROOT = [os.path.join(BASE_DIR,'vol/web/static')] My templates are at the root of the project.

TEMPLATES={
    ...
    'DIRS':[os.path.join(BASE_DIR, 'templates')],
    ...
}

This is how I call my bootstrap.min.css file in a project template:

<link rel="stylesheet" href="{% static 'assets/plugins/bootstrap/css/bootstrap.min.css' %}">

But it doesn't work because the file is not loaded. What's the right way to do it?


CodePudding user response:

Inside main urls.py

from django.conf import settings as SETTINGS
from django.conf.urls.static import static

urlpatterns  = static(SETTINGS.STATIC_URL, document_root=SETTINGS.STATIC_ROOT)
urlpatterns  = static(SETTINGS.MEDIA_URL, document_root=SETTINGS.MEDIA_ROOT)

settings.py

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
STATIC_URL = '/static/'
STATICFILES_DIRS = (BASE_DIR / 'static',)
STATIC_ROOT = (BASE_DIR / 'static'/ 'static')

CodePudding user response:

  1. If you store your js and css files in some folder, then you need to specify this folder in STATICFILES_DIRS django setting. For example:
STATICFILES_DIRS = [
  "/home/my-user/my-project/static",
]

Then Django will search this directory(ies) when looking up files specified in {% static ... %} tag. https://docs.djangoproject.com/en/4.0/ref/settings/#staticfiles-dirs

  1. If Django still unable to find particular file, try using findstatic management command for diagnostics. This will show in which directories django is actually looking for the file. For example:
python manage.py findstatic -v3 assets/plugins/bootstrap/css/bootstrap.min.css

https://docs.djangoproject.com/en/4.0/ref/contrib/staticfiles/#findstatic

  • Related