【问题标题】:OS X Django/Apache app not serving media files, works using python shellOS X Django/Apache 应用程序不提供媒体文件,使用 python shell 工作
【发布时间】:2017-08-26 01:12:06
【问题描述】:

我有一个简单的 Django 应用程序,它显示一堆视频的缩略图。该应用程序所做的第一件事是在MEDIA_ROOT 中查找所有缩略图文件。其他一切都是从那里滴下来的。我已经让 apache 正确地为我的网站提供服务,但是它在我的 media_root 中找不到文件。

views.py:

def index(request): 
    def walk_dir_tree(path, file_type):
        return[os.path.basename(x) for x in glob.glob(path + file_type,
                                                      recursive=True)]   
    # Gets all .jpgs in media_root
    all_thumb_files = sorted(walk_dir_tree(
        settings.MEDIA_ROOT + 'thumbnails/**/*', '.jpg'), reverse=True)
...
return HttpResponse(template.render(context, request))

settings.py 中的相关信息:

# URLs
STATIC_URL = '/static/'
MEDIA_URL = '/media/'

# Root Dirs:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
# Dev
MEDIA_ROOT = '/Users/user/Documents/temp/secureDash/'

http-vhosts.conf:

<VirtualHost *:80>

    LogLevel debug

    ServerName securedash
    ServerAdmin <email>


    Alias /static /Users/user/icloud/projects/secureDash/static
    <Directory /Users/user/icloud/projects/secureDash/static>
        Require all granted
    </Directory>

    Alias /media /Users/user/Documents/temp/secureDash
    <Directory /Users/user/Documents/temp/secureDash>
        Require all granted
    </Directory>

    <Directory /Users/user/icloud/projects/secureDash/secureDash>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess securedash python-path=/Users/user/icloud/projects/secureDash/secureDash python-home=/Users/user/Documents/VirtualEnvs/securedash
    WSGIProcessGroup securedash
    WSGIScriptAlias / /Users/user/icloud/projects/secureDash/secureDash/wsgi.py
</VirtualHost>

为了确保我的媒体路径正确,我编写了一个快速脚本并在 python3 shell 中运行它:

MEDIA_ROOT = '/Users/user/Documents/temp/secureDash/'

def walk_dir_tree(path, file_type):
    return[os.path.basename(x) for x in glob.glob(path + file_type,
                                                  recursive=True)]

all_thumb_files = sorted(walk_dir_tree(
    MEDIA_ROOT + 'thumbnails/**/*', '.jpg'), reverse=True)


# prints a list of all jpgs
print(walk_dir_tree('/Users/user/Documents/temp/secureDash/thumbnails/**/*', '.jpg')) 
# also prints a list of all jpgs
print(all_thumb_files) 

任何地方都没有记录错误。它每次只返回一个空列表。我认为这可能是我的 settings.py 或 apache 配置的问题,但我尝试了很多组合,但没有任何效果。

我在我的树莓派上的“生产”服务器上使用了这个精确的配置。我只是想建立一个更接近生产环境的开发环境。唯一的区别是操作系统和MEDIA_ROOT 的位置。

Django 1.10 | OSX 10.11 埃尔卡皮坦 |阿帕奇 2.4

【问题讨论】:

  • 你有没有在你的代码中尝试过一些很好的旧 print() 调试?看起来在index() 视图代码中的控制台上输出一些运行时值将为诊断提供足够的提示
  • 是的,我在任何我能想到的相关的地方都做过。 print(all_thumb_files) 返回 []。 print('settings.MEDIA_ROOT is: ' + settings.MEDIA_ROOT) 返回 /Users/user/Documents/temp/secureDash/,在 walk_dir_tree 内部:print('path plus file_type: ' + path + file_type) 返回 /Users/user/Documents/temp/secureDash/thumbnails/**/*.jpg
  • 是什么python版本? “在 3.5 版中更改:支持使用“**”的递归 glob。”
  • 与其做整个事情,不如考虑测试全局文件夹并确认那里有东西。前任。 glob.glob("/Users/user/Documents/temp/secureDash/thumbnails/HARD_CODE_A_FOLDER/*.jpg 并确认该文件夹中有内容。
  • 我的 virtualenv 使用的是 Python 3.5.2

标签: python django macos apache osx-elcapitan


【解决方案1】:

这最终成为/Users/user/Documents/temp/secureDash/ 的权限问题,但帮助我解决这个问题的是@mithuntnt 建议使用不同的文件夹。所以我回过头去搜索/Users/user/Documents,它确实返回了结果。然后添加返回结果的/temp。然后/secureDash 没有。我在/temp 中尝试了一个与/secureDash 处于同一级别的不同目录,并且有效。所以我检查了/secureDash 的权限,结果是:

17276808 drwx------@  6 user  _www                           204B Mar 29 15:04 secureDash

所以我跑了chmod -R 755 /Users/user/Documents/temp/secureDash/

现在缩略图正在加载。我不应该在 apache 日志中看到文件权限错误是问题所在吗?我在 http-vhosts.conf 中有 LogLevel debug。什么是适合开发环境的 LogLevel 设置?

【讨论】:

    猜你喜欢
    • 2012-09-30
    • 1970-01-01
    • 2011-06-16
    • 2011-06-02
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    相关资源
    最近更新 更多