apache服务器在Django项目中找不到静态文件

Posted

技术标签:

【中文标题】apache服务器在Django项目中找不到静态文件【英文标题】:apache server cant find static files in Django project 【发布时间】:2013-04-17 05:10:51 【问题描述】:

我正在尝试使用 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 页 - 为什么!?)

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),
)

【问题讨论】:

将 Django 应用程序投入生产的推荐方法是使用 mod_wsgi,正如它在:docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi 中所说的那样,您应该尝试一下 谢谢,我已经用 mod_python 工作了,但你能告诉我为什么推荐 mod_wsgi(根据你的经验),以后不想在 mod_python 上遇到问题 mod_python 现在有点死了。如果你开始任何新的使用 mod_wsgi,Django 也推荐。 就是这个原因。此外,最新更新的 Django 文档使用并推荐mod_wsgi,最好使用它,不要使用一些旧的不支持的模块。 【参考方案1】:

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

【讨论】:

非常感谢您的回复。我尝试了一些东西,现在它工作正常。这是我所做的和未做的: 1. 添加了您提到的别名和目录部分 2. 将此添加到我的 urls.py (我认为它实际上可以工作)如果 settings.DEBUG: urlpatterns += patterns( '', (r'^static/(?P.*)$', 'django.views.static.serve', 'document_root': settings.STATIC_ROOT), ) 3. 没有运行 collectstatic ,并且我将路径作为“/static/x”而不是 STATIC_URL x,我是否真的需要这样做,因为它似乎不适用于 STATIC_URL @user_2000 很高兴你能成功。如果我的回答有帮助请采纳。另一方面:当您查看输出时, STATIC_URL x 和 /static/x 'should' 应该是相同的。如果不是,请检查您的静态路径。我总是建议使用 STATIC_URL 而不是硬编码,从长远来看,它会为您节省很多痛苦!

以上是关于apache服务器在Django项目中找不到静态文件的主要内容,如果未能解决你的问题,请参考以下文章

在 django 1.9 中找不到我的静态文件

为啥 Aptana 3 (OS X) 在导入的项目中找不到 django 设置模块?

django - 看不到从 apache 上传的媒体文件

在 wsgi.py 的“django.py”中找不到引用“DjangoWhiteNoise”

在 django(使用 python-opencv)项目中找不到人脸(人脸检测)

Django:在 Traceback 中找不到异常