#配置静态文件权限,让apache有权访问
<Directory "E:/program/python2.5.1/Lib/site-packages/django/contrib/admin/media">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
#设置不缓存,这样修改python源代码以后,可以自己加载,正式发布的时候,这个要去掉
MaxRequestsPerChild 1
# file types we want to serve statically
# case insensative match
<LocationMatch "(?i)\.(jpg|gif|png|txt|ico|pdf|css|jpeg)$">
SetHandler None
</LocationMatch>
三、常见错误
3.1 常见错误现象1
关键词: client denied by server configuration。
表象: 访问了http://localhost:80/mysite/ 后,假如发现图片没有加载,或者css没有应用上,并且httpd.conf文件中Location的配置绝对路径肯定没错。
解释:
第一, 首先检查httpd.conf配置Alias的配置,
看看Alias设置的路径是否正确,
Alias /site_media c:/django/myproject/media
请保证您附加的这个路径肯定是你的图片或者css存放的路径。
其次,请检查您的django项目myproject目录下的settings.py,保证MEDIA_ROOT、STATIC_PATH、TEMPLATE_DIRS这三个参数指向的绝对路径正确无误。
最后,如果上面的参数都没错,却还是不行。那么请您检查Apache的日志文件
Apache2.2\logs\error.log
如果您发现有这样的错误提示:
错误日志
[Mon Nov 20 17:27:08 2006] [notice] Child 4172: Starting thread to listen on port 80.
[Mon Nov 20 17:27:08 2006] [error] [client 127.0.0.1] client denied by server configuration: c:/Django/myproject/media/css/global.css, referer: http://localhost:80/mysite/
那么说明是因为对这些资源文件的访问被拒绝了。
此时,请您浏览 http://localhost:80/site_media/css/global.css ,如果遇到了HTTP 403错误,就说明是权限问题。
此时,请到httpd.conf,增加如下配置来允许静态资源文件夹被访问:
增加的配置行
# 对需要访问的区域,可以增加正确的Directory块
# 否则会得到这样的错误:client denied by server configuration: c:/Django/myproject/media/css/global.css, referer: http:/ /localhost:80/mysite/
<Directory "c:/Django/myproject/media/">
Order Deny,Allow
Allow from all
</Directory>
重启Apache服务。
3.2 常见错误现象2
关键词: EnvironmentError: Could not import settings。
表象: 访问了http://localhost:80/mysite/ 后,直接页面报告如下错误:
页面错误输出
1. Mod_python error: “PythonHandler django.core.handlers.modpython”
Traceback (most recent call last):
File “C:\Python24\Lib\site-packages\mod_python\apache.py”, line 299, in HandlerDispatch
result = object(req)
File “c:\django_src\django\core\handlers\modpython.py”, line 163, in handler
return ModPythonHandler()(req)
File “c:\django_src\django\core\handlers\modpython.py”, line 125, in __call__
if settings.ENABLE_PSYCO:
File “c:\django_src\django\conf\__init__.py”, line 27, in __getattr__
self._import_settings()
File “c:\django_src\django\conf\__init__.py”, line 54, in _import_settings
self._target = Settings(settings_module)
File “c:\django_src\django\conf\__init__.py”, line 82, in __init__
raise EnvironmentError, “Could not import settings ‘%s’ (Is it on sys.path? Does it have syntax errors?): %s” % (self.SETTINGS_MODULE, e)
EnvironmentError: Could not import settings ‘myproject.settings’ (Is it on sys.path? Does it have syntax errors?): No module named myproject.settings