Django provides full support for anonymous sessions. The session framework lets you store and retrieve arbitrary data on a per-site-visitor basis. It stores data on the server side and abstracts the sending and receiving of cookies. Cookies contain a session ID – not the data itself.
We need to import certain stuffs before we go use session and session variables in django.
1. Edit the MIDDLEWARE_CLASSES in settings.py of your app and make sure it contains
'django.contrib.sessions.middleware.SessionMiddleware'.
2. Include 'django.contrib.sessions' in INSTALLED_APPS in settings.py of your app.
3. Include from django.contrib.formtools.wizard.views import SessionWizardView in views.py
We need to import certain stuffs before we go use session and session variables in django.
1. Edit the MIDDLEWARE_CLASSES in settings.py of your app and make sure it contains
'django.contrib.sessions.middleware.SessionMiddleware'.
2. Include 'django.contrib.sessions' in INSTALLED_APPS in settings.py of your app.
3. Include from django.contrib.formtools.wizard.views import SessionWizardView in views.py
Assume we have registration app which takes all user data and registers him/her. Now in login page we ask the user to enter the email id and password, and using that email id we try to extract the user assigned unique id and keep it in variable ra_no and then store it in session variable ra_no.
In the below code we have extracted both user full name and user unique id and store them in session variables,
m = s.objects.get(email__exact=request.POST['emailid'])
ra_no = m.uid
request.session['ra_no'] = ra_no
full_name = m.username
request.session['full_name'] = full_name
If we need to use this values stored in session we just need to use the below syntax, now uid contains the value stored in session variable.
uid = request.session['ra_no']
No comments:
Post a Comment