2013-05-05 132 views
1

我想加载一个CSS文件,但由于某种原因,它无法找到该文件。下面是我得到在加载文件中的错误:django - 无法加载静态CSS文件..错误

---[05/May/2013 11:31:58] "GET /static/css.css HTTP/1.1" 404 1625

File structure 


Folder structure 
     ecomstore 
      ecomstore 
       catalog 
       preview 
        Templates 
         Catalog 
         base.html 
      static 
       css.css 

urls.py

from django.contrib import admin 
import os 
admin.autodiscover() 

static = os.path.join(os.path.dirname(__file__), 'static') 
print 'static=',static 

urlpatterns = patterns('', 

    (r'^catalog/$','ecomstore.preview.views.home'), 
    url(r'^admin/', include(admin.site.urls)), 
    (r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root':static}), 

    (r'^',include('ecomstore.catalog.urls')), 
) 

base.html文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "XHTML1-s.dtd" > 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <title>DJANGO E-Commerce website</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>{% block title %}{% if page_title %}{{ page_title }} - {% endif %} ?  
      {{ site_name }}{% endblock %}</title> 
    <meta name="keywords" content="{{ meta_keywords }}" /> 
    <meta name="description" content="{{ meta_description }}" /> 

    <link rel="stylesheet" href="/static/css.css" 
     type="text/css" /> 

    <script src="/static/jquery.js" type="text/javascript"></script> 
</head> 
<body> 
    {% block site_wrapper %}{% endblock %} 
</body> 
</html> 

settings.py 

STATIC_ROOT = '' 

STATIC_URL = '/static/' 
+1

你不应该使用'{{STATIC_URL}}'在你的HTML CSS的href?看看[用静态文件应用在Django中应用css的问题](http://stackoverflow.com/a/5396625/1693859)。 – 2013-05-05 17:07:27

回答

0

删除此来自url的行s.py其并不需要在发展:

(r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root':static}), 

,它应该是固定的。

0

文件结构应该像这样:

. 
├── {app_name_1} 
│   ├── ... 
└── {app_name_2} 
    ├── static 
    │   └── {app_name_2} # Put static files in this folder 
    │    ├── css 
    │    ├── img 
    │    └── js 
    └── templates 
       └── {app_name_2} # Put templates in this folder 
+1

将'/ static /'更改为'static /'后,它工作。这些小错误很糟糕......但不知道如何调试这些东西。 – user1050619 2013-05-05 18:16:31