【发布时间】:2014-03-17 08:00:36
【问题描述】:
我尝试了很多东西,但无法提供 css 和图像。我正在尝试通过 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 的文档。
编辑: 在遵循 pythonvile 的答案后,我能够让 css 工作,但它仍然很奇怪。 我可以看到效果
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; }
【问题讨论】:
标签: python html css django django-templates