5

我正在尝试使用 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}),
)
4

1 回答 1

3

1)您是否在虚拟主机中正确设置了静态文件?我没有看到他们...

IE

    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 管理命令

于 2013-04-23T20:52:46.950 回答