So far the Documentation for Django has been too technical. How do I create a session and store variables in it or get variables from it? I'm new to the Django framework, hence why the Documentation is too technical. Sessions are my 'last step'.
+3
A:
Assuming you want database based sessions (Django also offers file based sessions, and cache based sessions):
- Open settings.py and find
MIDDLEWARE_CLASSES
. Add'django.contrib.sessions.middleware.SessionMiddleware'
to the list. - Find
INSTALLED_APPS
in the same file and add'django.contrib.sessions'
there. - Run
manage.py syncdb
from the command line.
After the initial setup you can use request.session
in your views to store information between requests.
For example this will store the information:
request.session['name'] = 'Ludwik'
and you can retrieve it as easly:
print request.session['name']
or
if request.session['name'] == 'Ludwik':
print 'you are awesome!'
For other things you can do with the request.session
object see the documentation.
Ludwik Trammer
2010-02-19 00:59:24
+2
A:
Ludwik's answer is the way to go, but if you want a gentler intro into the world of Django, the (free) Django Book (http://www.djangobook.com/en/2.0/) is a must-read. Chapter 14 deals with sessions (http://www.djangobook.com/en/2.0/chapter14/).
stevejalim
2010-02-19 08:57:22