For a questionnaire, I want to present a sequence of forms to the user. I'd like to keep the view generic, so that it can present any form instance in the sequence.
Currently, I'm storing a list of the form objects (not instances), and I instantiate each form as it needs to be presented. (e.g. formobject = formslist[3]; form = formobject();
).
Is there a more pythonic way of doing this? I've considered using a getnext
function in the definition of each form, but I still need a place to list the sequence of forms that I want to generate.
The next step will be to introduce some skip-logic, so hardwiring the form sequence is not ideal.
Maybe this will help. This is what I have in my view, using a getnext
function. It works from the first form to the second, but then does not serve the third form:
def showform(request):
if 'formobj' not in locals():
formobj = StartForm
if request.method == 'POST': # If the form has been submitted...
form = formobj(request.POST)
if form.is_valid():
try:
form.save()
except:
pass
cd = form.cleaned_data
formobj = form.get_next()
form = formobj()
if formobj == 'done':
render_to_response('doneform.html', context_instance=RequestContext(request))
else:
form = formobj()
else:
form = formobj()
return render_to_response('template.html', {'form': form, 'requestpath': request.get_full_path()}, context_instance=RequestContext(request))