0

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))
Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
Ari
  • 460
  • 6
  • 13
  • Have you looked at the [form wizard](https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/)? – jterrace Mar 08 '12 at 20:06
  • Yes. I've found the form wizard limited; it does not (as far as I can tell) save the input data from each step on the server. This is a risk for long forms if the user wants to stop filling out the form and come back later. – Ari Mar 08 '12 at 20:30
  • @Ari: have you used django-merlin? do you have some working code to share? – brain storm May 22 '14 at 05:41
  • @brainstorm, I don't recall if I got django-merlin working. For my application, I ended up making a static form based on Adobe FormFiller. – Ari May 22 '14 at 17:25
  • @Ari: is that a Django app or something outside of django? Thanks – brain storm May 22 '14 at 17:54
  • @brainstorm, it is outside of Django. Adobe has an application to create forms that you can then post online. – Ari May 30 '14 at 19:55

1 Answers1

3

Have you looked at this for saving data during form wizard steps

Using FormWizard and saving the forms data in between before the completion of the whole process?

Also mentioned in that post is an app called django-merlin which may do what you need. I've not personally used it though.

Edit

Here is another post that might be helpful to you Django - form wizard step by step

Community
  • 1
  • 1
darren
  • 18,845
  • 17
  • 60
  • 79
  • I have looked at both of these, but didn't quite get where I wanted to (or I don't quite understand how to do so). The first answer does save the data in a serialized format, but it seems like retrieving and unserializing is not trivial. It also is not clear that this will allow users to move backward in the form. Django-merlin is interesting, but seems to save data to the Session, not on the server side. I may be wrong about this, but haven't been able to tell from Merlin's docs. – Ari Mar 08 '12 at 21:32
  • I now see that Session data *is* stored on the server side. So it's worth another look--Merlin may be what I need. – Ari Mar 09 '12 at 07:00