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 开发者_C百科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.
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.
精彩评论