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
394 views
in Technique[技术] by (71.8m points)

python - django not serving static files

I'm using Django==1.5.5 and My django app is structured as

--project/
----project/
------settings.py
------urls.py
----app1/
------models.py
------views.py
----staticfiles/
------style.css
----static/
------admin/
--------js/
--------css/
------style.css
----templates/
------header.html
------post.html
------footer.html
----manage.py

the /project/settings.py is

import os    
DEBUG = False
TEMPLATE_DEBUG = DEBUG    
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')    
ALLOWED_HOSTS = ['localhost', '127.0.0.1']    
MEDIA_ROOT = ''    
MEDIA_URL = ''    
SITE_ROOT = PROJECT_ROOT    
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')    
STATIC_URL = '/static/'    
STATICFILES_DIRS = (
    os.path.join(SITE_ROOT, 'staticfiles'),
)    
TEMPLATE_DIRS = (
    os.path.join(SITE_ROOT, 'templates'),
)

in header.html i trying use it as:

    <head>
        <title>My Site</title>
        {% load static from staticfiles %}
        <link rel="stylesheet" href="{% static 'style.css' %}">
    </head>

But its not loading mysite.com/static/style.css and giving error 404

I ran python manage.py collectstatic to collect all static file.

Why its loading css file? any configuration missing?

Please suggest.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I had the same problem and none of the answers I found worked. It turns out that there are a few parts that must be set up correctly for static files to work. Note: I'm using django 1.6 and this solution is for development not deployment.

Setting up static file dirs

django-admin.py startproject $site_name
mkdir $site_name/static
mkdir $site_name/static/css
mkdir $site_name/static/javascript
mkdir $site_name/static/images

your folder should look like this

$site_name/
    manage.py
    $site_name/
        __init__.py
        settings.py
        urls.py
        wsgi.py
    static/
        css/
        javascript
        images/

edit the $site_name/$site_name/setting.py file and add

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS =( os.path.join(STATIC_ROOT, 'css/'),
                    os.path.join(STATIC_ROOT, 'javascript/'),
                    os.path.join(STATIC_ROOT, 'images/')
                  )

edit $site_name/$site_name/urls.py and add

from django.conf import settings
from django.conf.urls.static import static

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

using static urls

{% load staticfiles %}
<a href="{% static "banner.png" %}">

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

...