0

In Django, I want to create an image using an R function (so via Rpy2) based on user input (POST) and then display that image back to the user.

My approach: Save the image to a file and display it in the template (the same template as the form).

1) Is this the right approach?

I then found that sometimes when I submit the form a few times with different parameters, I get the same image back when I shouldn't, so some kind of caching is going (in the browser?). I was also concerned with accidentally passing the image created by one user to another simultaneous user.

So when the form is submitted, I add a random number to the name of the image, getting a new image name (and new image) every time.

2) Is this a reasonable approach?

I have an intuition that I'm doing things the stupid way, but I'm not sure what I should be doing.

DavidC
  • 1,409
  • 10
  • 25

1 Answers1

1

To get the browser cache, you can simply add a random query parameter of the url of the image, like this

<img src="assest/images/img1.png?randomstring/>

As for if it's a reasonable approach, it depends whether these image need to be re-generate every time the user visit the page. It this is the case, you'd better serve these images directly by a view, like this. Otherwise you will need to delete these images that will never be used again.

If somehow you do need to reuse these images, just remember there must be a mechanism that prevents your hard drive been used up.

Community
  • 1
  • 1
faceclean
  • 3,781
  • 8
  • 39
  • 58
  • I'm adding a random number from from (1,2,3,...,100) to the end of the image name--so I can't get more than 100 before they start saving over each other. – DavidC May 09 '11 at 01:33
  • You still haven't tell us, do u really need to save these pictures? I mean, do u need to cache them? As the for the name overlapping, just use something else other than (1 .. 100), like the time when the picture is generated, or a UUID. Or you can use this module : http://docs.python.org/library/tempfile.html – faceclean May 09 '11 at 02:05
  • Thanks. I do *not* need to be saving the pictures, but I didn't know what else to do. I didn't really understand the answers about serving images directly from a view, but I'll go back and try to read that again. – DavidC May 10 '11 at 18:11