我正在尝试使用 mod_python 在 apache 服务器上上传我的代码。我已经尝试了很多,但服务器无法访问我的静态文件(我所有的图像、js 和 css)。这是我的虚拟主机设置:
<VirtualHost *:80>
ServerName mysite.com
ServerAlias www.mysite.com
Alias /static/ /home/mysite/products/static/
#
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mysite\.com
RewriteRule (.*) http://mysite.com$1 [R=301,L]
#
DocumentRoot /home
<Directory /home/mysite/>
SetHandler mod_python
PythonHandler mod_python.publisher
PythonDebug On
</Directory>
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
我的访问日志:
"GET /giftproducts/static/js/top.js HTTP/1.1" 404 1487 "http://xxx.xxx.xx.xxx/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31"
xxx.xxx.xx.xxx - - [23/Apr/2013:15:09:52 -0500] "GET /giftproducts/static/css/index.css HTTP/1.1" 404 1486 "http://xxx.xxx.xx.xxx/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31"
(未找到 404 页 - 为什么!?)
设置.py:
import os
import sys
path = os.path.abspath(os.path.join(os.path.dirname(__file__)))
MEDIA_ROOT = path + '/products/media/'
MEDIA_URL = '/media/'
PROJECT_ROOT = path
STATIC_ROOT = path + '/products/static/'
STATIC_URL = '/products/static/'
STATICFILES_DIRS = (
path,
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
TEMPLATE_DIRS = (
path + '/products/templates',
) #this works, since it is loading the html
尝试给出“/static/x”和“{{ STATIC_URL }}x”之类的路径,但没有任何效果。
对此的任何帮助都会很棒。谢谢。
更新:除了下面的 Glyn 建议之外,我将这些行添加到我的 urls.py 中,然后它就起作用了。
if settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
)