0

My style.css is placed in appname/static/appname/.

My settings.py has this code:

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

And in my base.html I load it like this:

{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'appname/style.css' %}">

But the styles are not loading. If I remove STATICFILES_DIRS and change STATIC_URL = '/static/' to STATIC_URL = '/static/appname/', it works perfectly, but I guess it's not the best practice for the case I'll add any other app to the project later. What I might be doing wrong?

1
  • This should work, I mean your configuration. The line STATICFILES_DIRS = (os.path.join(BASE_DIR, "static/"),) means that django will search in the directory rootproject/static/, not in your apps static . What is your directory structure? Commented Sep 19, 2018 at 12:26

3 Answers 3

1

Just change one thing,

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

It will search in static folder inside your app. Also if you want to add a specific directory,

STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"), '/your specific directory/',
)

From here you can directly add the particular file name, and djnago will search in that specific directory.

1
  • "Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files". This is only needed if you want an extra directory besides the apps. Commented Sep 19, 2018 at 12:23
0

Remove "appname" in {% static 'appname/style.css' %}, you must not place it there because python knows automatically in which application the file is, it get the application name from the request

10
  • You should see the HTTP call in the django terminal, seek the relative to syle.css and copy it here
    – Fucio
    Commented Sep 19, 2018 at 10:27
  • is says "GET /static/style.css HTTP/1.1" 404 1651, seems like it doesnt check STATICFILES_DIRS...? Commented Sep 19, 2018 at 10:31
  • ad using the appname?
    – Fucio
    Commented Sep 19, 2018 at 10:33
  • Then it doesn't say anything regarding styles.css Commented Sep 19, 2018 at 10:35
  • System check identified no issues (0 silenced). September 19, 2018 - 13:34:26 Django version 2.1.1, using settings 'md5light.settings' Starting development server at 127.0.0.1:8000 Quit the server with CTRL-BREAK. [19/Sep/2018 13:34:34] "GET /appname/ HTTP/1.1" 200 1443 Commented Sep 19, 2018 at 10:35
0

By default django picks static directory from app's directory. So, if your static directory is inside app directory there is no need to specify STATICFILES_DIRS. Now /static/ will point to files and directories in the static directory of your app. To refer your css file use

{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'appname/style.css' %}">
1
  • make sure STATICFILES_DIRS = [] Commented Sep 19, 2018 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.