django: Force cache-refresh after update

Viewed 2238

After updating a model instance, I redirect back to this instance "detail page". Part of the model is an image generated from the models content. In this redirect I want to force the browser to reload the image from the server. I tried it with headers but that doesn't work:

response = HttpResponseRedirect('/target/path/')
response['Cache-Control'] =  'no-cache'
response['Pragma'] = 'no-cache'
return response

Because I assume the redirect loses the headers.

How can I force the page to reload the image but only after user returns from update page?

EDIT:

the image is served in an img tag. The src actually points to a server end-point that generates the image form the "id" in the src link. Then the image should be cached until it changes.

2 Answers

How are you serving the image? A good idea would be to add a hash on to the end of the filename for the image, and then when you update the image, change that hash. This way, when the image is updated the browser is forced to reload the image.

You could even just use a timestamp of when the file was last updated??

I've settled on using djangos

@last_modified(last_modified_func)

decorator. See here. The function can accept the same parameters as the decorated method (eg. the entity id in my case). With that I can simply look up the last modified meta data in the database and django will deal with the If-modified-since or If-unmodified-since headers from the request and either return a Not Modified response or the new image.

Related