Form Wizard: store the session values

103 views
Skip to first unread message

Karim

unread,
Feb 24, 2015, 8:16:59 PM2/24/15
to Django Users
When I click on the link for my form, I would like to restore the session values if the user accidentally left the wizard or other cases happened.

How I can restore the session and initialize the FormWizard with the session values? Everytime I click on the link to render the view I got an empty form.

Thank you. 

--
Karim N. Gorjux

Karim

unread,
Feb 25, 2015, 3:55:05 AM2/25/15
to Django Users
I'm trying to get the data from the session. The SessionWizard store the data in the session

session_data = request.session[wizard_key_form]['step_data']

at this point session_data is the dictionary with the data in the form filled by the user.

But this doesn't work

ServiceCreationWizard.as_view(
        FORMS,
        initial_dict=session_data,
    )(request)


Working with ipdb and django toolbar and reading the docs here[1] seems to me that the fields name in the session dictionary are a little different from the forma requested by the initial_dict

initial_dict wants something like

initial = {
...     '0': {'subject': 'Hello', 'sender': 'us...@example.com'},
...     '1': {'message': 'Hi there!'}
... }

but session are stored in this way

 u'step_data': {
      u'0': {u'0-subject': u'Hello', u'0-sender': u'us...@example.com'}
      u'1': {u'1-message': u'Hi there!'},
}

Do I have to convert all the keys name to pass the session values to the initial_dict? Maybe there is a better way to backup and restore the session?

Thank you





On Wed, Feb 25, 2015 at 12:14 PM, Karim <lemie...@gmail.com> wrote:
When I click on the link for my form,
​[...]
 
--
Karim N. Gorjux

Karim

unread,
Feb 25, 2015, 7:18:35 PM2/25/15
to Django Users
Solved with a very easy approach.

I override the WizardView get method and I use a session variable to understand if the user want to restore the edit or create a new form

   def get(self, request, *args, **kwargs):
        """
        This method override the WizardView handling the restore of the session
        if the value request.session['restore-wizard'] is True
        """
        try:
            if request.session['restore-wizard']:
                return self.render(self.get_form())
        except KeyError:
            # just call the default super get
            return super(ServiceCreationWizard, self).get(request, *args, **kwargs


--
Karim N. Gorjux
Reply all
Reply to author
Forward
0 new messages