0

In Django 1.7, I want a set of random numbers to be created each time my website is visited. To do this, I make the random numbers in my views.py file, in global scope. In this way, the random numbers are generated the first time the user loads the website and views.py is run.

However, if somebody has already visited my website, and then refreshes in the address bar to go back to the homepage, then these random numbers are never regenerated. So, the user is stuck with their old set of numbers.

Why are the commands in views.py only processed the first time? Is it possible to still have the random number generation in global scope, whilst also being called each time the address bar is refreshed?

Karnivaurus
  • 22,823
  • 57
  • 147
  • 247
  • 2
    Why do you need to store the random number globally if you want it to change each time the page/view changes? – Ramast Jun 06 '15 at 04:42

1 Answers1

0

I assume you want to generate a random number per user per access to your site.

Check how to create a middleware and this question.

Create another middleware that checks the last_visit datetime and if it is older than your threshold, update the random number holder with random.random() (or the method you want to generate it).

Community
  • 1
  • 1
cdvv7788
  • 2,021
  • 1
  • 18
  • 26