如何在 Django 中设置和获取会话变量? [英] How can I set and get session variable in django?

查看:34
本文介绍了如何在 Django 中设置和获取会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户登录发生时,我需要在会话上设置一个变量.我该怎么做?

I need to set a variable on session, when a user login happens. How can I do this?

if request.user.is_authenticated():
profile = request.user.get_profile()
request.session['idempresa'] = profile.idempresa

我的另一个问题是一种形式:

My other question is in a form:

class PedidoItensForm(ModelForm):
class Meta:
    model = ItensPedido

def __init__(self, *args, **kwargs):
    profile = kwargs.pop('vUserProfile', None)
    super(PedidoItensForm, self).__init__(*args, **kwargs)

如何获取会话的idempresa"值以在查询集中使用?

How can I get the "idempresa" value of session, to use in my queryset?

推荐答案

用于设置会话变量:

request.session['idempresa'] = profile.idempresa

获取会话数据:

if 'idempresa' in request.session:
    idempresa = request.session['idempresa']

这篇关于如何在 Django 中设置和获取会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆