How to set session variables during registration using django-registration and auth?

I use django-registration to register users in my application. This part works great. The part that I cannot understand is how to set user session variables when a user logs in. For example, I would like to populate variables containing UserProfile data, as well as the output of several other functions. Then I can use this information in subsequent views / templates.

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

I am using django 1.1 and Python 2.6

+7
django login django-authentication django-registration
source share
2 answers

If you do not need persistent storage of user data (only additional session data), see:

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

The session structure will most likely already be enabled if you use django.contrib.auth.

If you want to permanently store additional user data (not only in the 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

+3
source share

I understand that @stefanw provided you with 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 want to change is part of the contrib.django.auth application.

So your options will be as follows:

  • Create a small middleware class to set up session data.
  • Create a template tag or other bit of code that can be integrated into the login template or the next page in which the data you need is installed.
  • Write your own login function (it's really pretty simple, actually).

Happy django-ing!

+1
source share

All Articles