Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

turned out that django automatically appends the media_url in front of the imagefield url.

i was under impression that i have to append the media_url, which caused me to look for a simpler solution.

Django Admin only shows relative paths

![django admin shows relative paths][1]

http://i.stack.imgur.com/aZGPy.png

But, tastypie gives me absolute path

http://i.stack.imgur.com/SEdaX.png

so i now don't have to worry about urls anymore...thanks again guys :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top