我尝试过很多东西,但却无法提供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; }
答案 0 :(得分:2)
定义MEDIA_ROOT,STATIC_ROOT,STATICFILES_DIRS和TEMPLATE_DIRS时,需要使用绝对路径。好的做法是启动设置模块(它不必是单个文件!),如下所示:
import os
PROJECT_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
是的,我知道它的外观,到目前为止我遇到的最糟糕的Python片段。然而,这是一种可行的方式。让我们在shell中查看它:
In [1]: from django.conf import settings
In [2]: settings.PROJECT_DIR
Out[2]: '/home/yourname/Projects/simple_emp_hr/simple_emp_hr/..'
哪个等同于/home/yourname/Projects/simple_emp_hr
对吧?
现在我们可以再次使用os.path.join
将dirs粘在一起:
In [3]: import os
In [4]: os.path.join(settings.PROJECT_DIR, 'foo', 'bar')
Out[4]: '/home/yourname/Projects/simple_emp_hr/simple_emp_hr/../foo/bar'
在这里我们进入实际设置:
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static') # you will have something different, I guess
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'hr_base', 'static'),
)
MEDIA_ROOT的相同练习。
编辑:我的网址代码为静态。我在一天之内无耻地从一些博客中拿走了它。
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# normal patterns
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT, 'show_indexes': True}),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
)
答案 1 :(得分:-2)
你应该看看使用Django压缩器,http://django-compressor.readthedocs.org/en/latest/ 。 它完全值得学习曲线。解决了很多问题