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
added
PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
STATICFILES_DIRS = ( os.path.join(PROJECT_ROOT, "static"), )
and removed STATIC_ROOT
from settings.py
, It worked for me
Go to your HTML page load static by
{% load static %}
Now only mistake I've made was this
My code:
<img src="**{% static** "images/index.jpeg" %}" alt="My image">
Updated:
<img src=**"{% static 'images/index.jpeg' %}' alt="My image"**>
You get it right
Read this carefully: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/
Is django.contrib.staticfiles
in your INSTALLED_APPS
in settings.py
?
Is DEBUG=False
? If so, you need to call runserver
with the --insecure
parameter:
python manage.py runserver --insecure
collectstatic
has no bearing on serving files via the development server. It is for collecting the static files in one location STATIC_ROOT
for your web server to find them. In fact, running collectstatic
with your STATIC_ROOT
set to a path in STATICFILES_DIRS
is a bad idea. You should double-check to make sure your CSS files even exist now.
DEBUG = True
in my local settings did it for me.
Are these missing from your settings.py
? I am pasting one of my project's settings:
TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.contrib.messages.context_processors.messages")
Also, this is what I have in my urls.py
:
urlpatterns += patterns('', (
r'^static/(?P<path>.*)$',
'django.views.static.serve',
{'document_root': 'static'}
))
You can just set STATIC_ROOT depending on whether you are running on your localhost or on your server. To identify that, refer to this post.
And you can rewrite you STATIC_ROOT configuration as:
import sys
if 'runserver' in sys.argv:
STATIC_ROOT = ''
else:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'