Question

I'm using django-registration to log users into my application. That part works fine. The part that I cannot figure out is how to set custom session variables when the user logs in. For instance, I'd like to populate variables containing UserProfile data as well as the output of a few other functions. Then I'd be able to use that information in subsequent views/templates.

If anybody can point me to a tutorial online or post some sample code, that would be great.

I'm using django 1.1 and Python 2.6

Was it helpful?

Solution

If you don't want persistent storage of user data (just additional session data) have a look at:

http://docs.djangoproject.com/en/dev/topics/http/sessions/

The sessions framework will most probably be already enabled if you use django.contrib.auth.

If you want persistent storage of additional user data (not only in a session, but in the database), you will store them in another "profile" model:

http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

OTHER TIPS

I realize @stefanw provided you an alternative solution, but to answer the original question:

Setting session data at login is difficult because the easiest place to set that data is in your view function, and the particular view function you'd want to modify is a part of the contrib.django.auth app.

So your options would be the following:

  • Create a small middleware class to set your session data.
  • Create a template tag or other bit of code than can be integrated into the login template or subsequent page that will set the data you want.
  • Write your own custom login view function (it's really quite easy, actually).

Happy django-ing!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top