Django Loading Static Files?
Django is not loading my static files. However it is loading my templates which are in the static folder. Also chrome is not seeing the static files either I'm not even getting a 4
Solution 1:
The approach I take with static files is basically what's outlined in the docs.
In local development, Django will serve static files automatically from the directory specified in STATIC_ROOT
as long as django.contrib.staticfiles
is in your INSTALLED_APPS
and DEBUG = True
My project structure typically looks like this:
my_project/
some_app/
lib/
static/ <------- STATIC_ROOT - used in production. `collectstatic` collects here
static_assets/ <- STATICFILES_DIRS - used in local dev
css/
less/
js/
images/
templates/ <---- TEMPLATE_DIRS
manage.py
settings.py is typically:
INSTALLED_APPS = (
. . .
'django.contrib.staticfiles',
. . .
)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static_assets'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
Then in templates, you can again use the staticfiles app's template tags to build out paths to static files:
{% load static from staticfiles %}
<link href="{% static 'css/normalize.css' %}" />
Also note that with <link>
tags, you need to use the href
property for the url instead of src
.
Solution 2:
In your setting.py file add this
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
create a folder in your app named static and in your template file add this {% load static %}
Post a Comment for "Django Loading Static Files?"