我試過了很多東西,但只是不能提供css和圖像。我試圖通過CSS呈現背景圖像。以下是代碼的網站Django 1.5.4無法在開發服務器上服務css
simple_emp_hr/
|->hr_base/
| |-> views.py
| |-> templates/hr_base/homepage.html
| |-> static
| |->css/base.css
| |->images/foto-camion-transformers.jpg
|
|->simple_emp_hr/
|->urls.py
|->settings.py
以下爲觀點:
@csrf_protect
def index(request):
print(request.GET)
print(request.POST)
template = loader.get_template('hr_base/homepage.html')
c = Context()
c.update(csrf(request))
c.update({'user':request.user.username})
str = template.render(c)
response = str
print(str)
hres = HttpResponse()
hres.write(response)
return hres
以下是我的urls.py:
from django.conf.urls import patterns, include, url
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^hr_base/index$','hr_base.views.index'),
)
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indices':True}),
)
以下是我的settings.py:
# Django settings for simple_emp_hr project.
DEBUG = True
TEMPLATE_DEBUG = DEBUG
MANAGERS = ADMINS
ALLOWED_HOSTS = []
SITE_ID = 1
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = 'static_root/'
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
"hr_base/static/hr_base/",
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'simple_emp_hr.urls'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
"../simple_emp_hr/hr_base/templates/",
"../simple_emp_hr/emp_users/templates/",
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'hr_base',
)
我的CSS文件有:
body {
background-image: url ("images/foto-camion-transformers.jpg"); }
title {
background-color: rgb(238,62,128);
color: white; }
以下是homepage.html:
<!DOCTYPE html>
<html>
<head>
<title> Simple Employee HR : Solutions for the companies which are <em>simple</em></title>
<link href="{{ STATIC_URL }}css/base.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h1>Simple Employee HR : Solutions for the companies which are <em>simple</em></h1>
<hr />
<h2 id="top">Who we are</h2>
<hr />
<a href="#top">top</a>
</body>
</html>
在查看打印可以讓我打印呈現的HTML:
<!DOCTYPE html>
<html>
<head>
<title> Simple Employee HR : Solutions for the companies which are <em>simple</em></title>
<link href="css/base.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h1>Simple Employee HR : Solutions for the companies which are <em>simple</em></h1>
<hr />
<h2 id="top">Who we are</h2>
<hr />
</body>
</html>
[17/Feb/2014 14:40:17] "GET /hr_base/index HTTP/1.1" 200 1455
[17/Feb/2014 14:40:17] "GET /hr_base/css/base.css HTTP/1.1" 404 3640
我從來沒有得到的CSS呈現。瀏覽器顯示純HTML文本。什麼可能是錯的? 我是django新手,想學習。我在這裏無能爲力,無法理解django的文檔。
編輯: 我能夠得到css工作後的pythonvile的答案,但它仍然奇怪。 我能看到的
h1, h2 {
color: #ee3e80;}
的影響,但我不能看到
h1 {
background-color: rgb(238,62,128);
color: white; }
或
效果body {
background-image: url ("images/foto-camion-transformers.jpg"); }
title {
background-color: rgb(238,62,128);
color: white; }
檢查我的控制檯上:[18/Feb/2014 12:01:04]「GET /hr_base/css/base.css HTTP/1.1」404 3640。它沒有意義 – preetam
它應該從靜態開始。它很容易混淆哪個目錄被哪個url訪問。你能檢查我之前發佈的url文件,並讓我知道如果那可以嗎? – preetam
這變得更糟了,我將html轉儲到控制檯進行調試。 {{STATIC_URL}}在那裏變得沒有任何東西。它的字面上沒有。 – preetam