Wednesday, June 1, 2011

Re: Chained forms

On Wed, Jun 1, 2011 at 9:52 AM, bruno desthuilliers
<bruno.desthuilliers@gmail.com> wrote:
> On May 31, 6:55 pm, Marc Aymerich <glicer...@gmail.com> wrote:
>> Hi!,
>> I'm writing an admin action.
>> This action needs to collect some information before execute the "final
>> task".
>> In order to collect this information the user should fill 3 consecutive
>> forms, (once they submitted one, another is rendered).
>>
>> My question is,
>> How can I store the form fields state submitted on each form, and retrieve
>> this fields states on the final form, before execute the 'final task'?
>
>
> This is a well-known problem, with 2 well-known solutions: 1/ store
> the state in hidden fields or 2/ use sessions.
>
> The form-wizard contrib app mentionned by DrBloodmoney uses the first
> approach, which makes it "more general" since you can use it without
> having to resort to sessions. OTHO, I didn't find it quite as easy to
> use and flexible as I would have liked, specially when I had to fork
> it to give users the possibility to navigate backward. You should
> nonetheless give it a try first and see if it works for you. If it
> doesn't, you may want to roll your own session-based solution.
>
> HTH
>


Thanks DrBloodmoney and bruno,
I try to use wizard forms in order to accomplish this. But I have some
problems:
I don't know why but when I execute the action, it jumps directly to
step 2 and when I submit this step 2, it returns to the admin
changelist, ignoring the last step. Also the wizard seems to ignore
the initial data.

Anyone see something wrong in my code?
it is based on this snippet:
http://ten.ynottony.net/2011/01/using-django-formwizard-in-a-view/

class OrderAdmin(admin.ModelAdmin):
actions = ['bill_orders',]

def bill_orders(modeladmin, request, queryset):
from datetime import datetime
from django.template import RequestContext
initial = {0: { 'bill_point': datetime.now(),
'fixed_point': True,
},}
form = ContactWizard([b1, b2, b3], initial=initial)
return form(context=RequestContext(request),request=request)


from django import forms
from django.shortcuts import render_to_response
from django.contrib.formtools.wizard import FormWizard

class b1(forms.Form):
bill_point = forms.DateField(required=False)
fixed_point = forms.BooleanField(required=False)
force_next = forms.BooleanField(required=False)
create_new_open = forms.BooleanField(required=False)

class b2(forms.Form):
BILL_OR_PRICING_CHOICES = (
(True, 'Bill this ordres too'),
(False, 'Only use for Pricing'),)
effect = forms.ChoiceField(choices=BILL_OR_PRICING_CHOICES, required=False)

class b3(forms.Form):
bill_point2 = forms.DateField(required=False)

class ContactWizard(FormWizard):
def done(self, request, form_list):
return render_to_response('done.html', {
'form_data': [form.cleaned_data for form in form_list],})

def get_template(self, step):
return ['billing/wizard_%s.html' % step, 'billing/wizard.html']

Thanks!
--
Marc

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment