【发布时间】:2013-12-15 11:48:35
【问题描述】:
我正在使用Django==1.5.5,我的 django 应用程序的结构为
--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 我尝试将其用作:
<head>
<title>My Site</title>
{% load static from staticfiles %}
<link rel="stylesheet" href="{% static 'style.css' %}">
</head>
但它没有加载 mysite.com/static/style.css 并给出错误 404
我运行python manage.py collectstatic 收集所有静态文件。
为什么要加载css文件?是否缺少任何配置?
请提出建议。
【问题讨论】:
-
您是通过内部开发服务器运行 django 还是通过网络服务器作为 nginx 或 apache 运行 django?
-
您要转向生产吗?
DEBUG = False时,需要自己处理静态文件。只有DEBUG = Truedjango 才会为你处理静态文件。 -
@BurhanKhalid 有什么建议吗?
-
请看serving static files in production;如果您处于开发模式(使用
runserver),则设置DEBUG = True。