Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set and get session variable in django?

Tags:

django

session

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?

like image 781
fh_bash Avatar asked Jan 22 '13 18:01

fh_bash


People also ask

Can a user set a session variable?

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.

How do I use file based sessions in Django?

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.

Where is Django 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).


1 Answers

For setting session variable:

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

For getting session Data:

if 'idempresa' in request.session:     idempresa = request.session['idempresa'] 
like image 113
Varnan K Avatar answered Sep 22 '22 01:09

Varnan K