Background
I read this section in Django’s documentation titled “Selecting the current time zone,”, and it’s clear to me how a user can select their preferred timezone for their session (and then the middleware makes sure it is used throughout their session):
- developer prepares a dictionary of timezones for users to select from
- developer makes this list available to user in a form within a template that is served by a
GET
request to a view calledset_timezone
- user selects timezone using this form and makes a
POST
request set_timezone
view uses a simple one-liner to set session"django-timezone"
variable:request.session["django_timezone"] = request.POST["timezone"]
- Finally, the custom
TimezoneMiddleware
class referenced in the documentation will then activate the timezone for all view requests in the session from that point on
Question
If I store a user’s timezone preference in a User model or User profile model, what is the best way to make sure the "django-timezone"
session variable is always set for all their sessions (each time they log in)?
Thoughts/guesses
- Would I need to modify a built-in Django authentication middleware class of some sort?
- Would it be best to extend the Django-standard
LoginView
class fromdjango.contrib.auth.views
? - Or will the aforementioned
LoginView
class already handle this on its own if I set up a timezone storing field in my User model correctly? (or something like this?)