FormWizard inside view with proper context handling and site templating support, without having to use urls.py

Okay - so I came across a really annoying problem earlier, where I wasn't able to easily load a formwizard as a segment into an existing view, and wrap it using my existing site template layouts. This was REALLY annoying. Especially since I wanted to keep as much of a 'overall' templating and application logic in the views.py (and just leave the forms.py to handle the form and its own templating for the form pages)

I spent about 2 hours trying to make this as conventional as possible, and finally came up with a solution. The result is something which looks as similar to the usual functionality. This also meant that there is seperation between form styling and overall site styling, so your forms can be used between multiple sites, and if your overall site template uses extends, then the context support keeps this nicely in order.

This also allows you to initialise the formwizard in a nicer way.. Of course, in each file, you'll need to import the necessary bits (like importing the testform from the view etc)

######### libs.py ##################################### This gives you the extra functionality you'll need ######################################################fromdjango.contrib.formtools.wizardimportFormWizardclassFormWizardSnip(FormWizard):def__init__(self,context):# Override the extra contextself.extra_context=context# Call the original initsuper(FormWizardSnip,self).__init__(self._forms)def__call__(self,request):returnsuper(FormWizardSnip,self).__call__(request)defrender_to_wizard(f,request,context):c=f(context=context)returnc(request)######## forms.py ##################################### This is the place where the forms go######################################################fromdjangoimportformsfromdjango.shortcutsimportrender_to_responsefromwebapp.libsimportFormWizardSnipfromdjango.templateimportRequestContextclasstestform_step1(forms.Form):subject=forms.CharField(max_length=100)sender=forms.EmailField()classtestform_step2(forms.Form):message=forms.CharField(widget=forms.Textarea)classtestform(FormWizardSnip):# We define the form list here, rather than in the __init__() call. MUCH nicer!!!_forms=[CreateNewGateway_step1,CreateNewGateway_step2]defget_template(self,step):return'wizard_step%s.html'%stepdefdone(self,request,form_list):# Compile extra contento=self.extra_contexto['form_data']=[form.cleaned_dataforforminform_list]# Build the request contextcontext=RequestContext(request,o)returnrender_to_response('wizard_done.html',context_instance=context)######## views.py ##################################### This is the view function which your urls.py has hit######################################################fromlibsimportrender_to_wizardfromformsimporttestformdeftest(request):# perform your application logic here (user testing etc)# Build the request context (this can be used by your overall site templates etc)context={'templateOptions':'etc etc'}# Call the correct parent function with the correct contextreturnrender_to_wizard(testform,context=context,request=request)Thatsit!Ihopethishelpssomeone!