2013-04-13 43 views
0

感谢寻找到这一点,因为它是让我的神经现在:)Django的静态文件链接

我不能让一个style.css文件(或任何静态文件,以获得oared在所有!),我有到处搜索,到目前为止还没有解决方案。

我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/' 

我不明白我做错了什么(或者事实上为什么服务器这些静态服务器必须如此尴尬)。任何帮助湖人的条款将不胜感激!

谢谢, blargie-BLA

+0

这是一个真正的服务器上,或者你只是运行'./manage.py runserver'? – hobbes3

+0

只是一个本地主机 –

回答

1

在您的服务器,你可能需要尝试这个

python manage.py collectstatic -l 

-l的一个符号链接,而不是复制。这可以防止相同文件的两个副本并节省一些空间,但也意味着如果您重命名原始文件,则必须重新建立链接。

有关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文件夹。

0

尝试:

{% load static %} 
<link rel="stylesheet" href="{% static 'css/style.css' %}" type="text/css" />