django - 无法加载静态css file..error

时间:2013-05-05 15:42:01

标签: 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/'

2 个答案:

答案 0 :(得分:0)

从urls.py中删除此行,因为它在开发中不需要:

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

应该修复它。

答案 1 :(得分: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