djangophotologuemedia-url

Is the auto generated photologue image urls overridable?


I have two separate servers. One is hosting django instance on "www.example.com", the other is serving static files on "static.example.com". In django settings, MEDIA_URL points to "static.example.com". Therefore my photologue is using the "static.example.com" domain.

I want to use "www.example.com" for my photologue, but I can't figure out how I can override the MEDIA_URL setting for the photologue. Do you know any way to solve this?

Note: Django is serving over apache, Static files hosted on AWS (Amazon Web Service). I just want to change only photologue domain. Other static files must stay where they are.


Solution

  • Unfortunately Photologue doesn't provide a setting for this.

    But looking at the code I found a workaround that should do the trick. I assume you use the get_SIZE_url() methods. All of those methods call self.cache_url(). So, you could create a subclass of ImageModel (or Photo if you want to have the functionality that comes with it) which overrides this method.

    from photologue.models import ImageModel  # or Photo
    
    class LocalPhoto(ImageModel):   # or Photo
    
        def cache_url(self):
            local_path = os.path.dirname(self.image.url)[7:].split('/', 1)[1]
            return '/%s/%s' % (local_path, 'cache')
    

    If you extended Photo you already have a working get_absolute_url() otherwise an implementation is straight forward.

    Django admin uses get_admin_thumbnail_url() so if you define a PhotoSize with name 'admin_thumbnail' the above method should work there too.

    Effects have a method sample_url() used to show the effect on a sample image in Django admin. This method is based on MEDIA_URL so it won't work. Unfortunately in this case you cannot simply override this method. But since these images won't change you could upload them to AWS.