我正在尝试使用mod_python将代码上传到apache服务器上。我已经尝试了很多,但是服务器无法访问我的静态文件(所有图像,js和css)。
这是我的Virtualhost设置:
<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页,为什么!?)
settings.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}),
)
最佳答案
1)您是否在虚拟主机中正确设置了静态文件?我没看到他们...
即
Alias /media/ /products/static
Alias /static/ /products/static
<Directory /products/static>
Order allow,deny
Allow from all
</Directory>
2)在模板中,始终使用
{{ STATIC_URL }}
检索静态文件,这是最佳做法。3)添加
django.contrib.staticfiles
并运行collectstatic
management command