主要看看httpd.conf:
MaxRequestsPerChild 1
LoadModule python_module modules/mod_python.so
<Location "/">
SetHandler python-program
PythonPath "['D:/py'] + sys.path"
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite.settings
PythonAutoReload Off
PythonDebug On
</Location>
Alias /sitemedia D:/py/mysite/media
<Directory "D:/py/mysite/media">
Order allow,deny
Allow from all
</Directory>
<Location "/sitemedia">
SetHandler None
</Location>
<LocationMatch "\.(jpg|gif|png|css|js)$">
SetHandler None
</LocationMatch>
添加和修改上面这些就可以了,别的不要动
MaxRequestsPerChild设为1特别有用,不用重启Apache就可以看到程序修改的结果
PythonPath "['D:/py'] + sys.path"注意加的是D:/py,也就是我们运行django-admin.py startproject mysite时所在的目录
SetEnv DJANGO_SETTINGS_MODULE mysite.settings中mysite是project名
Alias /sitemedia D:/py/mysite/media的意思是把media目录设置成为/sitemedia这样的URL访问方式,紧接着的Directory给任何人访问该目录下文件的权限,默认无权限
然后Location告诉Apache /sitemedia下是静态内容不用python解释
然后LocationMatch告诉Apache列表中结尾的文件是静态内容不用python解释
当我们需要引入其它站点的jpg或css时这个LocationMatch就有用了
我们设置模板的绝对路径来访问:
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates".
# Always use forward slashes, even on Windows.
'D:/py/mysite/templates',
)
然后模板目录下的html就可以使用下面的方式访问静态内容了:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>测试页面
</title>
<meta name="description" content="python,Django" />
<link href="/sitemedia/stylesheet/blue.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
{{ content }}
<a href="http://hideto.iteye.com">hideto@蛙眼</a>
<img src="http://hideto.iteye.com/upload/user/logo/34344/f5e9dd3a-a090-4697-828b-edb8836c221a.jpg"></img>
</body>
</html>
注意css的引用方式和img标签