Cuong Tran Cuong Tran - 1 month ago 7
Python Question

Can't load Django static files in view

I'm a new player in Django and Python. I want to load the static files from the project level static folder. The problem is I can't but I still can load the static of an app.
This is the structure of my project:

Yaas
__init__.py
settings.py
urls.py
wsgi.py
templates
base.html
static
style.css
auction
templates
auction
index.html
static
auction
style.css
__init__.py
apps.py
models.py
urls.py
views.py


The thing is it works with static file of auction, like this in base.html:

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


But it returns 404 with the project static file:

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


settings.py

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


I tried other solutions and document on Django but it still didn't work. I am using Django 1.10.

jga jga
Answer

I made this work by adding the following:

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

From the docs (after point 4):

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.

Please note that the STATIC_ROOT directory, where Django will output all collected static files found in each <app>/static and STATICFILES_DIRS directories, must be different.

Hope that helps!