开发者

2 questions on django sessions

I have two problems. Since they are related to each other, I'm asking them in one post.

1) Is it a good practice to use sessions extensively in a Django app? I made a quiz app, where each question is made on the fly and stored in the session. When the user answers the first question, the second question is generated and saved in the same session variable as the first and so on. I also use sessions to keep track of the number of correct answers, answ开发者_开发知识库ered questions, the ID of the next question, and some other variables. Of all the django apps I have seen, mine although small, is filled with request.session's. Is it normal? Or do you suggest a better way?

2) Since this app is all session based, if I open a new tab in the same browser, and enter the url of the quiz, it'll pick up from where the other tab was left. How can I restrict tabs from seeing each other?

Thanks


1) Is it a good practice to use sessions extensively in a Django app?

I think you should re-formulate your question to consider if the very nature of sessions is appropriate for what you are trying to achieve. Understand how sessions work in Django. The session id is stored in cookie, so basically the session lifetime and behavior is directly influenced by the nature of cookies.

Put yourself in the mind of a user that doesn't understand when and how (or even if) cookies are manipulated. If you end up with "I don't care if the system doesn't remember me for some abstract technical reason (such as closing my browser, manually reset by clearing my browser's history and accidentally erasing cookies, etc.)." then the session my be a good way to go.

IMHO, Django makes it so easy to store the questions in a database that I usually go for account creation and database use without asking myself the question.

2) Since this app is all session based, ... How can I restrict tabs from seeing each other?

Then you shouldn't use Django sessions because they are based on cookies, which are the real culprit behind this behavior. Since this is a quiz-type thing, I'm assuming you have at least 1 form on each page. Generate some kind of custom-session ID and store it in a hidden form field. You can also store it in a query string, but that is not very "Django-ish".


If you do not need data persistence beyond what sessions provide, then it's perfectly fine and normal to use only sessions. It's probably obvious, but if you wanted to save results, questions and answers in relation to logged in users, you'll need to delve into Django's models at the very least.

You could always use a URL parameter (stored in request.GET) to differentiate between browser tabs; if a new page is loaded without the URL parameter, generate a random value and redirect to the URL with the random value appended as a URL parameter. Use the URL parameter value as a part of your session key structure to differentiate browser tab data.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜