Home > other >  Change image src url using HttpResponse in Django views
Change image src url using HttpResponse in Django views

Time:11-12

Is there any way of redirecting an image's 'request' URL in Django views, to an Nginx server so that Django doesn't have to serve the image itself? The background to my questions has to do with image tracking, which for obvious reasons requires a trip to Django.

For example, I have a webpage with an image, say;

<img src="https://mydjangoserver.com/uuid/image-url/image.jpg">

Which is passed to views which strips the UUID and src url for the image is changed to the NGINX server, say,

https://my-nginx-server.com/image.jpg

The image is then loaded within the original html page that first requested the image.

Any advice on how to redirect the image src url to avoid it being served by Django would be very much appreciated.

CodePudding user response:

The answer as it turns out was simple:

def image_redirect(request, image):
    return redirect(f'https://my-nginx-server.com/{image}')

Thank you @Tim Schilling better-simple.com for your help.

  • Related