我是django的新手,目前我正在學習並嘗試使用django-cms構建基本站點。下面是我的項目文件夾結構和代碼文件django中的靜態url路徑設置
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
標記是錯誤的還是settings.py
中的路徑設置是錯誤的?
考慮這個頁面https://docs.djangoproject.com/en/dev/howto/static-files/#using-django-contrib-staticfiles 這一個 https://docs.djangoproject.com/en/dev/howto/static-files/#staticfiles-in-templates 還可以修改您的URL模式: '從django.contrib.staticfiles.urls 導入staticfiles_urlpatterns #...其餘的URLconf去這裏... urlpatterns + = staticfiles_urlpatterns() ' –
是啊我已經通過每個鏈接和書面代碼根據教程,我已經提到了上面的整個代碼,因爲我是無法找到爲什麼模板不能渲染/使用css文件? –
您是否在settings.py中將'django.core.context_processors.static''添加到了'TEMPLATE_CONTEXT_PROCESSORS'中? 看看這裏 https://docs.djangoproject.com/zh/dev/howto/static-files/#with-a-context-processor –