开发者

Django upload file url problem

开发者 https://www.devze.com 2023-03-30 00:12 出处:网络
I\'ve got this media root:MEDIA_ROOT = \'/var/www/hosted/myapp/static/\' and this media urlMEDIA_URL = \'/static/res/\' and I am trying to upload a file using a callable: def get_uploadto(instance, fi

I've got this media root:

MEDIA_ROOT = '/var/www/hosted/myapp/static/'
and this media url
MEDIA_URL = '/static/res/'
and I am trying to upload a file using a callable:
def get_uploadto(instance, filename):
    ''' Dummy callable to silence the upload_to field of FileFields '''
    return os.path.开发者_StackOverflow中文版join('uploads', filename)
, it uploads the picture on the disk in /var/www/hosted/myapp/static/uploads/ just as planned, but in the admin it creates a link poiting to static/res/uploads, which does not exist. Does anyone know why? Of course, I can simply move the uploads dir within static res, but I am looking for a more elegant solution.


This is the expected behaviour. The files are in

MEDIA_ROOT + 'uploads' + filename
or
/var/www/hosted/myapp/static/uploads/filename

the URL's that point to those files are:

MEDIA_URL + 'uploads' + filename
or
/static/res/uploads/filename

You then need to make sure that your application or hosting environment points the URL to the on disk location. In the dev environment you would have something like this in your urlpatterns:

(r'^static/res/(?P<path>.*)$', 'django.views.static.serve', {'document_root': os.path.dirname(settings.MEDIA_ROOT)}),
0

精彩评论

暂无评论...
验证码 换一张
取 消