views:

35

answers:

2

I am looking to return a random number between 0 and 4 in a Django view, which is repeatedly called. The number is limited in that it can't be the same as the number that was called previously. It would be fine if the number loops rather than being random, it just can't be the same as what was returned before.

I tried using a variable outside of the view, and incrementing it within the function, but that wouldn't save between each call to the view.

I hope that explanation works, I am brand new to Django.

+2  A: 

What you are looking for are sessions.

Using sessions in django: http://docs.djangoproject.com/en/dev/topics/http/sessions/

General explanation of sessions: http://en.wikipedia.org/wiki/Session_(computer_science)

In a nutshell: store things in request.session, use it like any old dictionary.

Swizec Teller
A: 

It isn't clear from your question in what context you want to be sure the value doesn't repeat. For example, do you mean the same browser shouldn't see repeats, or globally, the same URL shouldn't repeat no matter who hits it?

For browsers, use sessions. They are persisted dictionaries identified by browser cookies. If you need it to be global, then you'll have to use the database or a global cache like memcache.

Ned Batchelder