jazzband / django-formtools

A set of high-level abstractions for Django forms
https://django-formtools.readthedocs.io
BSD 3-Clause "New" or "Revised" License
801 stars 135 forks source link

KeyError: wizard_signup_view #103

Open realmhamdy opened 7 years ago

realmhamdy commented 7 years ago

Hi there

So I'm using the SessionWizardView for the first 2 steps of a 3 step signup process. My issue is the same as the one mentioned on this stackoverflow question. Here's a copy of my stack trace:

Traceback:  

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/decorators.py" in inner
  185.                     return func(*args, **kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/formtools/wizard/views.py" in dispatch
  244.         response = super(WizardView, self).dispatch(request, *args, **kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/formtools/wizard/views.py" in post
  305.                 return self.render_done(form, **kwargs)

File "/app/members/views.py" in render_done
  59.         return super(SignupView, self).render_done(form, **kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/formtools/wizard/views.py" in render_done
  362.         self.storage.reset()

File "/app/.heroku/python/lib/python2.7/site-packages/formtools/wizard/storage/base.py" in reset
  33.         wizard_files = self.data[self.step_files_key]

File "/app/.heroku/python/lib/python2.7/site-packages/formtools/wizard/storage/session.py" in _get_data
  13.         return self.request.session[self.prefix]

File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/sessions/backends/base.py" in __getitem__
  57.         return self._session[key]

Exception Type: KeyError at /members/venues/signup/
Exception Value: 'wizard_signup_view'

It seems to happen randomly and in bursts of successive errors from some user. I'm not doing anything special in my render_done() override appearing there on the trace. Just a variable assignment. Any help debugging this would be appreciated. Thanks!

bram2w commented 6 years ago

Better late then never, but I had the same problem today. I am using the SessionWizardView for user signup and when the users fills out the form everything goes as it should. When the user is created I automatically login the user. If a user if already logged in and tries to create a new account, Django recognizes that the user is already logged in and logs the existing user out when logging the new user in. When the existing user is logged out it flushes the whole session, including the wizard_signup_view data. After that the SessionWizardForm also tries to delete the wizard_signup_view data, but it does not exists and throws an error.

I fixed this problem by adding

if self.request.user.is_authenticated():
    del self.request.session[SESSION_KEY]
    self.request.user = AnonymousUser()

just before logging in the user after completing the signup form, so that Django does not logout the existing user and therefore not flushes the session.

cwfoo commented 5 years ago

This error is triggered when you flush the session in the wizard's done method. Check if your method does any of these:

To temporarily work around this problem, avoid flushing the session in the wizard's done method. If you're having this problem in a user sign up form, consider serving the sign up form to logged out users only by redirecting logged in users.