from django.core.urlresolvers import reverse from django.shortcuts import render, redirect from django.contrib.formtools.wizard.views import (NamedUrlSessionWizardView, NamedUrlCookieWizardView) class RootStepWizard(object): """ A wizard mixin for use cases with a wizard step on the frontpage of your site -- with a request path of ``'/'``. Just define the name of the step (e.g. ``root_step = 'landing'``) and it does the setup and redirection automatically. Example:: # views.py class LandingWizardView(RootStepNamedUrlSessionWizardView): root_step = 'landing_page' # urls.py landing_forms = ( ('landing_page', LandingPageForm), ('welcome', WelcomeForm), ('account', AccountForm), ) landing_wizard = LandingWizardView.as_view(landing_forms, url_name='landing_step') """ root_step = None def get(self, *args, **kwargs): """ Redirects to the root path if needed and sets the step variable if on the root path. """ if self.request.path == '/': kwargs['step'] = self.root_step elif self.request.path == reverse(self.url_name, kwargs={'step': self.root_step}): return redirect('/') return super(RootStepWizard, self).get(*args, **kwargs) def get_step_url(self, step): """ Returns a root path for the configured step. """ if step == self.root_step: return '/' return super(RootStepWizard, self).get_step_url(step) class RootStepNamedUrlSessionWizardView(RootStepWizard, NamedUrlSessionWizardView): pass class RootStepNamedUrlCookieWizardView(RootStepWizard, NamedUrlCookieWizardView): pass