开发者

Where can I save a variable to be used between calls to a Django view?

I am looking to return a random number between 0 and 4 in a Django view, which is repeatedly开发者_C百科 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜