感谢您调查此事,因为它现在让我很紧张:)
我无法获得 style.css(或任何静态文件!),我用 google 搜索了一下,到目前为止没有一个解决方案有效。
我的 urls.py:
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
urlpatterns = patterns('',
url(r'^$', 'ports.views.home', name='home'),
)
urlpatterns += staticfiles_urlpatterns()
if settings.DEBUG:
urlpatterns += patterns('django.contrib.staticfiles.views',
url(r'^static/(?P<path>.*)$', 'serve'),
)
我的 base.html,我在其中加载静态数据:
{% load staticfiles %}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="static/css/style.css" type="text/css" />
...
我的 settings.py 引用静态目录:
STATIC_URL = '/static/'
我不明白我做错了什么(或者实际上为什么服务器这些静态数据必须如此尴尬)。任何湖人方面的帮助,我们将不胜感激!
谢谢, 布拉吉布拉
最佳答案
在你的服务器上,你可能需要试试这个
python manage.py collectstatic -l
-l
创建符号链接(symbolic link)而不是复制。这可以防止同一文件的两个副本并节省一些空间,但也意味着如果您重命名原始文件,则必须重新建立链接。
有关 collectstatic 命令的更多信息 here .
不要忘记设置 STATIC_ROOT
这里仅供引用是我的settings.py
对于那部分:
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(os.path.dirname(__file__), '..', 'static').replace('\\', '/')
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
这适用于 Django 1.4+,其中 settings.py
在(例如)mysite/mysite/
下而且它与 static
不在同一级别文件夹。
关于css - Django 静态文件链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15989416/