2

我是 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
         ....... 

设置.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',
)
......
.......

网址.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>

主页.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 %}

新样式.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标签中给出的路径错误还是标签中的路径设置settings.py错误?

4

1 回答 1

5

网址.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()
于 2013-02-05T11:41:20.073 回答