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
我已经在openshift中成功部署了我的Django项目。但我需要能够提供用户上传的文件。我为此用户MEDIA_ROOT和MEDIA_URL。我在这里遵循了这个教程,但没有任何反应。我不得不改变MEDIA_ROOT,因为我认为那是不正确的。所以我的MEDIA_ROOT看起来像
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
我在/ wsgi文件夹中添加了.htaccess,如文中所述
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
在我的urls.py中我添加了
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.
但我仍然不能为他们服务。我也尝试过不在urls.py中包含django静态方法,但结果相同。
In another tutorial .htacces is placed inside static folder. Am I doing something wrong?
在另一个教程中.htacces放在静态文件夹中。难道我做错了什么?
3 个解决方案
#1
8
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
只是为了让其他人知道,我通过将RewriteRule添加媒体文件夹更改为规则的第二部分来解决我的问题,所以它变成了
RewriteEngine On
RewriteRule ^application/media/(.+)$ /static/media/$1 [L]
Hope it helps others.
希望它能帮助别人。
#2
2
The problem is your media url. The symlink is created at wsgi/static/media, then your MEDIA_URL need is MEDIA_URL = '/static/media/'
问题是你的媒体网址。符号链接在wsgi / static / media中创建,然后您的MEDIA_URL需要是MEDIA_URL ='/ static / media /'
First step, on build script .openshift/action_hooks/build:
第一步,在构建脚本.openshift / action_hooks / build上:
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
Second step: In your settings:
第二步:在您的设置中:
MEDIA_URL = '/static/media/'
if ON_PAAS:
MEDIA_ROOT = os.path.join(os.environ.get('OPENSHIFT_DATA_DIR'), 'media')
else:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
#3
1
I've had the same problem but Apostolos' solution above hasn't solved the issue: when I try to access http://<domain>/media/<file>
it still doesn't work.
我遇到了同样的问题,但上面的Apostolos解决方案还没有解决问题:当我尝试访问http://
However it does work in another way. if I try to access the same file as if it was a static using http://<domain>/static/media/<file>
then it does work. This happens even without employing Gpzim98's workaround. I think the reason is simply that the media files are now accessible through the symbolic link. In other words media files don't get served but you can now pretend they are static files.
但它确实以另一种方式工作。如果我尝试使用http://
Would be very keen to understand what is going on and how to resolve this problem fully (if it can be done) and be able to serve media files directly through the MEDIA_URL.
非常希望了解正在发生的事情以及如何完全解决这个问题(如果可以的话)并且能够直接通过MEDIA_URL提供媒体文件。
Thanks
谢谢
#1
8
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
只是为了让其他人知道,我通过将RewriteRule添加媒体文件夹更改为规则的第二部分来解决我的问题,所以它变成了
RewriteEngine On
RewriteRule ^application/media/(.+)$ /static/media/$1 [L]
Hope it helps others.
希望它能帮助别人。
#2
2
The problem is your media url. The symlink is created at wsgi/static/media, then your MEDIA_URL need is MEDIA_URL = '/static/media/'
问题是你的媒体网址。符号链接在wsgi / static / media中创建,然后您的MEDIA_URL需要是MEDIA_URL ='/ static / media /'
First step, on build script .openshift/action_hooks/build:
第一步,在构建脚本.openshift / action_hooks / build上:
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
Second step: In your settings:
第二步:在您的设置中:
MEDIA_URL = '/static/media/'
if ON_PAAS:
MEDIA_ROOT = os.path.join(os.environ.get('OPENSHIFT_DATA_DIR'), 'media')
else:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
#3
1
I've had the same problem but Apostolos' solution above hasn't solved the issue: when I try to access http://<domain>/media/<file>
it still doesn't work.
我遇到了同样的问题,但上面的Apostolos解决方案还没有解决问题:当我尝试访问http://
However it does work in another way. if I try to access the same file as if it was a static using http://<domain>/static/media/<file>
then it does work. This happens even without employing Gpzim98's workaround. I think the reason is simply that the media files are now accessible through the symbolic link. In other words media files don't get served but you can now pretend they are static files.
但它确实以另一种方式工作。如果我尝试使用http://
Would be very keen to understand what is going on and how to resolve this problem fully (if it can be done) and be able to serve media files directly through the MEDIA_URL.
非常希望了解正在发生的事情以及如何完全解决这个问题(如果可以的话)并且能够直接通过MEDIA_URL提供媒体文件。
Thanks
谢谢