Search code examples
djangoauthenticationsession-variableschange-password

Alternative to session variable to sore data between views?


EDIT 16/07/2021

No response so far so I will try to make my question more precise if possible

As all session variables are reset when I call PasswordChangeView I need to store session variables content elsewhere (?) and re-set all my session variable once password has been changed (at the end of form_valid() function).

hope this is more clear and someone could drive me throught a solution...


I have override Django PasswordChangeView using Ajax (JQuery Form pluggin) and it works except all session variables are reset.

Reading Django documentation, I understand it is the "normal behavior" but I need to reinitialize some session data ; if not, it would have no real interest changing password using Ajax...

At start, I thought that call update_session_auth_hash function was responsible for that behavior but it seems not to be true as in form_valid function, request.user is set but request.session data are reset, even before fom is saved...

So my question is WHEN/WHERE I can retrieve session variable to be re-set when PasswordChangeView is called?

class PasswordChangeView(auth_views.PasswordChangeView): 
 
    def form_valid(self, form):
        print('request.user',self.request.user) # return connected user
        print('request.session.get("selected_database")',self.request.session.get("selected_database")) # return None
        self.object = form.save()
        update_session_auth_hash(self.request, self.object) # prevent user’s auth session to be invalidated and user have to log in again 
        print('request.session.get("selected_database")',self.request.session.get("selected_database")) # return None
        return JsonResponse ({'data': form.is_valid()},status = 200)

Solution

  • I have found a solution using cookies ; that may be obvious for many but I post in case it help.