【发布时间】: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