I’ve followed the instructions in this question, the documentation, and I’ve even looked at this one, but so far I’m unable to get at my static files using python manage.py runserver.
These are my (relevant?) settings:
STATIC_ROOT '/home/wayne/programming/somesite/static'
STATICFILES_DIRS ('/home/wayne/programming/somesite/static/styles',
'/home/wayne/programming/somesite/static/admin')
In my urls.py:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# The rest of my urls here...
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns()
I have the following code in my base.html
<link rel="stylesheet" type="text/css" href="{% static "styles/main.css" %}">
{% block styles %}
{% for sheet in styles %}
<link rel="stylesheet" type="text/css" href="{% static sheet %}">
{% endfor %}
And I promise I’m not hallucinating:
(.env)wayne:~/programming/somesite/static$ pwd
/home/wayne/programming/somesite/static
(.env)wayne:~/programming/somesite/static$ ls
admin styles
However, when navigate to http://localhost:8000/ my site is missing its stylesheets, and when I go to http://localhost:8000/static/styles/main.css I get 'styles/main.css' could not be found, and trying to navigate to localhost:8000/static/, or localhost:8000/static/styles it tells me that Directory indexes are not allowed.
What am I missing here?
Django’s handling of static files continue to be slightly confusing, particularly in terms of the naming of relevant settings.
The short answer is to move your static files; instead of
put them in
(where “yourapp” is obviously the name of your main application).
The longer answer is that I think you’ve (understandably) become confused about the various settings.
STATIC_ROOTonly refers to the location where your static files should end up after runningmanage.py collectstatic. You don’t need this set (as you shouldn’t really needcollectstatic) when developing locally. Either way,STATIC_ROOTshould always refer to an empty directory.Your
STATICFILES_DIRSsetting would almost work, except that you’ve told Django there are two paths where it should find static filesso when you do
{% static "styles/main.css" %}it will look forand will obviously not find them. What might work is
but there’s no need to do that, as you can just rely on
django.contrib.staticfiles.finders.AppDirectoriesFinder(in the default STATICFILES_FINDERS) and move your static files to an app directory.Hope this clears things up a little.