Django 会话:您可以检查会话数据并将其设置在同一视图中吗?
使用 Django 会话 时,查看会话数据是否是一个好习惯先前已设置,如果不设置初始会话数据,在同一视图中?
如果用户禁用了 cookie,这会对我或用户造成重大问题吗?
(测试 cookie 的存在令人困惑但是,如果您使用这些,您将无法记录有关用户最初查看的页面的数据。)
下面是我的views.py 文件的摘录。在视图中,用户正在访问一个包含游戏的页面。
如果他们之前存储过会话数据 - 并且如果是第一次查看游戏 - 视图将修改会话数据以记录他们访问过游戏的事实。如果他们没有会话数据,则会在此处为他们进行初始化。
def game(request, game_name):
game = get_object_or_404(Game, web_name=game_name)
c = { 'game': game }
# game_votes is used to store if user has voted on game yet.
# Also a key in dictionary indicates user has previously visited that game.
game_votes = request.session.get('game_votes', False):
if game_votes:
if not game_votes.has_key(game_name):
game_votes[game_name] = False
request.session['game_votes'] = game_votes
request.session.modified = True
else:
pass
else: # I.e. no session was declared previously.
request.session['game_votes'] = { game_name: False }
request.session['sorting_choice'] = 'PO'
request.session['ip_address'] = request.META['HTTP_X_FORWARDED_FOR']
return render_to_response('game.html', c)
由于这是我第一次使用 Django,我想知道在使用会话方面我无意中犯了哪些明显的错误。
非常感谢您的专业知识和帮助:)
编辑:
所以只是为了检查:如果用户禁用了 cookie,这不会在数据库中为他查看的每个页面创建一个新的会话条目?正确的?
When using Django sessions, is it good practice to see if session data has been previously set, and if not to set the initial session data, in the same view?
Will this cause major issues, for me or users, if users have their cookies disabled?
(The presence of test cookies has confused me a little. However if you used these you would not be able to record data about the page the user initially viewed.)
Below is an excerpt from my views.py file. In the view the user is visiting a page with a game on it.
If they have previously stored session data - and if first time viewing game - the view will modify session data to record the fact they have visited game. If they have no session data it will be initialised here for them.
def game(request, game_name):
game = get_object_or_404(Game, web_name=game_name)
c = { 'game': game }
# game_votes is used to store if user has voted on game yet.
# Also a key in dictionary indicates user has previously visited that game.
game_votes = request.session.get('game_votes', False):
if game_votes:
if not game_votes.has_key(game_name):
game_votes[game_name] = False
request.session['game_votes'] = game_votes
request.session.modified = True
else:
pass
else: # I.e. no session was declared previously.
request.session['game_votes'] = { game_name: False }
request.session['sorting_choice'] = 'PO'
request.session['ip_address'] = request.META['HTTP_X_FORWARDED_FOR']
return render_to_response('game.html', c)
As this is the first time I've used Django, I wanted to know what glaring mistakes I have inadvertently made with regards to using sessions.
Thank you very much for your expertise and help :)
EDIT:
So just to check: if a user has cookies disabled, this doesn't create a new session entry in the database for every page he views? Right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是完全正确的。会话数据不存储在 cookie 中 - cookie 只是将用户数据的唯一 ID 存储在会话数据库中。
一个小问题:不要使用
has_key
- 在 Python 中多年来一直不鼓励使用它。使用在
:This is exactly right. Session data isn't stored in cookies - the cookie just stores the unique ID of the user's data in the session database.
One minor nit: don't use
has_key
- it has been discouraged in Python for years. Usein
:在单个视图中检查会话,然后在同一视图中设置变量是完全可以的 - 大多数时候,如果我需要在几个不同的视图中需要这些数据,我会创建一个私有函数(只需使用名称前面的下划线)并调用它并将请求参数也传递给函数,因此它可以访问会话数据。然后,在每个需要访问会话数据的视图中,我确保在开始时调用此函数,以便数据始终可用。
It is perfectly fine to check for sessions in a single view and then set the variables in the same view - most of the time, if I am going to need this data in a few different views I'd make a private function (just with an underscore in front of the name) and call it and pass the request parameter to the function too, so it has access to the session data. Then in every view that needs access to session data, I make sure to call this function at the start so the data will always be available.