开发者

Django recommended practice for session info

开发者 https://www.devze.com 2023-03-08 19:39 出处:网络
Where should the \'session-related stuff\' be han开发者_运维知识库dled. For example: # Set a session value:

Where should the 'session-related stuff' be han开发者_运维知识库dled. For example:

# Set a session value:
request.session["fav_color"] = "blue"

# Get a session value -- this could be called in a different view,
# or many requests later (or both):
fav_color = request.session["fav_color"]

Should this be done in the view? in __init__? in the model? What if, for example, I am storing a large number of session variables that will be accessed by a number of views/pages?


Well, session requires the availability of a request object. So, that pretty much necessitates it be handled in the view in some form or fashion. The whole "Fat Model, Skinny Controller" mantra tends to scare people off from doing much in the view (which is essentially the controller in Django). However, the guide to follow is that business logic belongs in the model, but view logic belongs in the view. Sessions are inherently view-related, so I don't see any problem working with them there.

How much data you're storing in the session is pretty inconsequential, at least in the sense of where the logic goes. (It does matter somewhat in terms of page load and such.)

0

精彩评论

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