Django不提供静态文件 [英] django not serving static files

查看:82
本文介绍了Django不提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Django == 1.5.5 并且我的django应用的结构为

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

/project/settings.py

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'),
)

header.html 中,我尝试将其用作:

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>

但其未加载 mysite.com/static/style.css 并给出错误 404

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

我运行了 python manage.py collectstatic 收集所有静态文件。

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

为什么要加载CSS文件?是否缺少任何配置?

Why its loading css file? any configuration missing?

请提出建议。

推荐答案

根据< href = https://docs.djangoproject.com/en/dev/howto/static-files/ rel = nofollow noreferrer> docs 加载静态文件的正确方法是

According to docs right way to load static file is

{% load staticfiles %}
<img src="{% static 'my_app/myexample.jpg' %}" alt="My image"/>

这将起作用

这篇关于Django不提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆