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)
How can I get the "idempresa" value of session, to use in my queryset?
The $_SESSION is stored entirely on the server, so the user cannot modify it. However, it is possible for session-hijacking exploits where the user gets connected to another user's session.
If you want to use a database-backed session, you need to add 'django. contrib. sessions' to your INSTALLED_APPS setting. Once you have configured your installation, run manage.py migrate to install the single database table that stores session data.
Django uses a cookie containing a special session id to identify each browser and its associated session with the site. The actual session data is stored in the site database by default (this is more secure than storing the data in a cookie, where they are more vulnerable to malicious users).
For setting session variable:
request.session['idempresa'] = profile.idempresa
For getting session Data:
if 'idempresa' in request.session: idempresa = request.session['idempresa']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With