I am encountering a problem in setting up the URLs in Django.
To serve my media files I have this Amazon S3 bucket:
https://somebucket.s3.amazonaws.com/
I set the media URL inside settings.py
as follows:
MEDIA_URL = https://somebucket.s3.amazonaws.com/media/
Inside the urls.py
I set the code as follows:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_URL}));
My intention is that, when I point an image file from, lets say <img src="/media/image.jpg"/>
it will be automatically pointed to http://somebucket.s3.amazonaws.com/media/image.jpg
How can I do that? I have tried many methods but it always returns a 404.
However if i try to access the file directly http://somebucket.s3.amazonaws.com/media/image.jpg
it works.
Why would you want to do that? That defeats most of the purpose of having the external storage in the first place. It means that for every media request, it has to go through Django itself, to resolve the URL and generate the redirect to S3, with all the overhead that implies.
Instead, as sneawo suggests in the comments, you should simply set the img src attribute to point to the image via the S3 URL.