Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to serve django static files on development server - not found

Tags:

python

django

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?

like image 550
Wayne Werner Avatar asked Aug 19 '12 11:08

Wayne Werner


People also ask

Can Django serve static files?

Django also provides a mechanism for collecting static files into one place so that they can be served easily. Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT .

What server does Django use for development?

Django provides a simple WSGI server as a dev server.


1 Answers

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

/home/wayne/programming/somesite/static

put them in

/home/wayne/programming/somesite/yourapp/static

(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_ROOT only refers to the location where your static files should end up after running manage.py collectstatic. You don't need this set (as you shouldn't really need collectstatic) when developing locally. Either way, STATIC_ROOT should always refer to an empty directory.

Your STATICFILES_DIRS setting would almost work, except that you've told Django there are two paths where it should find static files

/home/wayne/programming/somesite/static/styles
/home/wayne/programming/somesite/static/admin

so when you do {% static "styles/main.css" %} it will look for

/home/wayne/programming/somesite/static/styles/styles/main.css
/home/wayne/programming/somesite/static/admin/styles/main.css

and will obviously not find them. What might work is

STATICFILES_DIRS = ('/home/wayne/programming/somesite/static',)

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.

like image 160
supervacuo Avatar answered Feb 05 '23 08:02

supervacuo