Django以打开的速度提供媒体文件(用户上传的文件) [英] Django serving media files (user uploaded files ) in openshift

查看:117
本文介绍了Django以打开的速度提供媒体文件(用户上传的文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地部署了我的Django项目在openshift。但我需要能够提供用户上传的文件。我用的是MEDIA_ROOT和MEDIA_URL。我在这里跟随这个教程,但没有发生任何事情。我不得不改变MEDIA_ROOT,因为我认为这是不正确的。所以我的MEDIA_ROOT看起来像

  MEDIA_ROOT = os.path.join(os.environ.get('OPENSHIFT_DATA_DIR','') ,'media')
MEDIA_URL ='/ media /'

我添加了.htaccess在/ wsgi文件夹中,如文中所述

  RewriteEngine On 
RewriteRule ^ application / media /(.+ )$ / static / $ 1 [L]

并创建了构建脚本来创建媒体的符号链接在文章中说。

 #!/ bin / bash 
如果[! -d $ OPENSHIFT_DATA_DIR / media];然后
mkdir $ OPENSHIFT_DATA_DIR / media
fi

ln -sf $ OPENSHIFT_DATA_DIR / media $ OPENSHIFT_REPO_DIR / wsgi / static / media
/ pre>

在我的urls.py中,我添加了

  urlpatterns + = static(settings.MEDIA_ROOT,document_root = settings.MEDIA_URL)

但我还是不能服务他们我也尝试在urls.py中不包含django static方法,但结果相同。



另一个教程.htacces放在静态文件夹中。我做错了吗?

解决方案

只是为了别人知道,我解决了我的问题,通过更正RewriteRule添加媒体文件夹到规则的第二部分,所以它成为

  RewriteEngine On 
RewriteRule ^ application / media /(.+)$ / static / media / $ 1 [L]

希望它帮助别人。


I have successfully deployed my Django project in openshift. But I need to be able to serve files that are uploaded by users. I user MEDIA_ROOT and MEDIA_URL for that. I followed this tutorial here, but nothing happened. I had to change MEDIA_ROOT because the one suggested there isn't correct i think. So my MEDIA_ROOT looks like

MEDIA_ROOT = os.path.join(os.environ.get('OPENSHIFT_DATA_DIR', ''),'media')
MEDIA_URL = '/media/'

I added the .htaccess in /wsgi folder with as it says in the article

RewriteEngine On
RewriteRule ^application/media/(.+)$ /static/$1 [L]    

and created the build script to make symbolic link of the media in static as the article says.

#!/bin/bash
if [ ! -d $OPENSHIFT_DATA_DIR/media ]; then
    mkdir $OPENSHIFT_DATA_DIR/media
fi

ln -sf $OPENSHIFT_DATA_DIR/media $OPENSHIFT_REPO_DIR/wsgi/static/media

In my urls.py I have added the

urlpatterns += static(settings.MEDIA_ROOT, document_root=settings.MEDIA_URL)

but I still can't serve them. I also tried not to include the django static method in urls.py but the same result.

In another tutorial .htacces is placed inside static folder. Am I doing something wrong?

解决方案

Just for others to know, I solved my problem by correcting the RewriteRule adding media folder to the second part of the rule, so it became

RewriteEngine On
RewriteRule ^application/media/(.+)$ /static/media/$1 [L]  

Hope it helps others.

这篇关于Django以打开的速度提供媒体文件(用户上传的文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆