我在runserver
本地机器 (Mac OS X) 上运行 Django 的开发服务器 ( ),无法加载 CSS 文件。
以下是 settings.py 中的相关条目:
STATIC_ROOT = '/Users/username/Projects/mysite/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/Users/thaymore/Projects/mysite/cal/static',
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
INSTALLED_APPS = (
# other apps ...
'django.contrib.staticfiles',
)
在我的 views.py 中,我请求上下文:
return render_to_response("cal/main.html",dict(entries=entries),context_instance=RequestContext(request))
在我的模板中{{ STATIC_URL }}
正确呈现:
<link type="text/css" href="{{ STATIC_URL }}css/main.css" />
变成:
<link type="text/css" href="/static/css/main.css"/>
这是文件实际所在的位置。我还跑去collectstatic
确保收集了所有文件。
我的 urls.py 中还有以下几行:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
我是 Django 的新手,所以可能缺少一些简单的东西——不胜感激。