Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...