0

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.

Yeo
  • 11,416
  • 6
  • 63
  • 90
dtjokro
  • 122
  • 7

2 Answers2

0

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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Hi Daniel, its because i'm using AngularJS to populate the image urls. so, i think its a bit ugly of having a media_url tag in front of the angular expression tag. but its solved now. thanks! – dtjokro Feb 16 '13 at 01:03
0

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]

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

But, tastypie gives me absolute path

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

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

dtjokro
  • 122
  • 7