django中的静态URL路径设置

时间:2013-02-05 10:25:44

标签: python css django django-cms

我是django的新手,目前我正在学习并尝试使用django-cms构建一个基本站点。下面是我的项目文件夹结构和代码文件

project_folder
    manage.py 
    main_folder 
         static
             css
                new-styles.css
             js
                new-styles.js
         templates                      
             base.html
             home.html
         media
             pic_1.gif 
             .....
         settings.py
         urls.py
         ....... 

settings.py

import os
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
DEBUG = True
MEDIA_ROOT = os.path.join(PROJECT_DIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR,'static')
STATIC_URL = '/static/'
TEMPLATE_DIRS = (os.path.join(PROJECT_DIR,'templates'))
TEMPLATE_CONTEXT_PROCESSORS = (
       'django.contrib.auth.context_processors.auth',
       'django.core.context_processors.i18n',
       'django.core.context_processors.request',
       'django.core.context_processors.media',
       'django.core.context_processors.static',
       'django.core.context_processors.debug',
       'django.contrib.messages.context_processors.messages',
       'cms.context_processors.media',
       'sekizai.context_processors.sekizai',
)
......
.......

urls.py

from django.conf.urls import patterns, include, url
from django.conf import settings

urlpatterns = patterns('',
            url(r'^admin/', include(admin.site.urls)),
            url(r'^$', ...........),               

if settings.DEBUG:
    urlpatterns = patterns('',
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
    url(r'', include('django.contrib.staticfiles.urls')),
) + urlpatterns

base.html文件

{% load cms_tags sekizai_tags menu_tags %}
<html>
  <head>
    <title>{%block "title" %}{% endblock %}</title>
    <link rel="stylesheet" href="{{ STATIC_URL }}css/new-styles.css">
  {% render_block "css" %}
  </head> 
  <body>
  {% cms_toolbar %}
    {% block "base_content" %}
    {% endblock %}
  {% render_block "js" %}  
  </body>
</html>

home.html的

{% extends "base.html" %}
{% load cms_tags %}
{% block "title" %}Welcome to Services{% endblock %}
{% block "base_content" %}
  <p>Hi this is paragraph</p>
  <img src="{{ MEDIA_URL }}images/promo3.jpg" />
 {% placeholder "number_one" %} 
 {% endblock %}

新-styles.css的

body {  
    padding:0px; margin:0px; margin-top:40px;
        background-color:#b0c4de;
     }
p {
      background-color:#e0ffff;
  }     

以上是我完整的项目结构和代码文件,但我的实际问题是new-styles.css文件不起作用,即使我在文件中写了一些css代码但它没有链接到模板,所以可以有人请告诉我,这是错误的,为什么base.html模板文件无法访问new-styles.css文件,link标记中给出的路径是错误的还是{{1}中的路径设置错了?

1 个答案:

答案 0 :(得分:2)

<强> urls.py

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from project_name import settings

admin.autodiscover()

urlpatterns = patterns('',
    .....................
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()