I\'m running Django\'s development server (runserver
) on my local machine (Mac OS X) and cannot get the CSS files to load.
Here are the relevant entries
Add the following code to your settings.py
:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
After that, create the static folder at the root directory of your project.
To load the static files on templates use:
{% load static %}
<img src="{% static "images/index.jpeg" %}" alt="My image"/>
These steps work for me, just see Load Static Files (CSS, JS, & Images) in Django
I use Django 1.10.
static
on the same level of settings.py
, my settings.py
's path is ~/djcode/mysite/mysite/settings.py
, so this dir is ~/djcode/mysite/mysite/static/
;static_dirs
and static_root
in static
, that's ~/djcode/mysite/mysite/static/static_dirs/
and ~/djcode/mysite/mysite/static/static_root/
;write settings.py
like this:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'mysite', 'static', 'static_root')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'mysite', 'static', 'static_dirs'),
)
do this command $ python manage.py collectstatic
in shell;
create a folder css
in static_dirs
and put into your own .css
file, your css file' path is ~/djcode/mysite/mysite/static/static_dirs/css/my_style.css
;
change <link>
tag in .html
file: <link rel="stylesheet" type="text/css" href="{% static 'css/my_style.css' %}">
,
Finally this link's path is http://192.168.1.100:8023/static/css/my_style.css
Bingo!
See if your main application (where the static directory is located) is included in your INSTALLED_APPS.
Files are searched by using the enabled finders. The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.
For recent releases of Django, You have to configure static files in settings.py
as,
STATIC_URL = '/static/' # the path in url
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
and use it with static template tag,
{% load static %}
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
I had to use
STATICFILES_DIRS = ( '/home/USERNAME/webapps/django/PROJECT/static/', )
That helped me.
Two most Basis points to be noted for running Static files in Django Application are - Declare static file path in your settings.py
file
STATIC_URL = '/static/'
Another important parameter is the web page in which you are using static keyword, you need to load the static files.
{% load static %}