views:

2328

answers:

4

I want to implement a simple 2 part FormWizard. Form 1 will by dynamically generated something like this:

class BuyAppleForm(forms.Form):
   creditcard = forms.ChoiceField(widget = forms.RadioSelect)
   type = forms.ChoiceField(widget = forms.RadioSelect)
   def __init__(self,*args, **kwargs):
        user = kwargs['user']
        del kwargs['user']

        super(BuyAppleForm, self).__init__(*args, **kwargs)

        credit_cards = get_credit_cards(user)
        self.fields['creditcard'].choices = [(card.id,str(card)) for card in credit_cards]

        apple_types= get_types_packages()
        self.fields['type'].choices = [(type.id,str(type)) for type in apple_types]

This will dynamically create a form with lists of available choices.

My second form, I actually want no input. I just want to display a confirmation screen containing the credit card info, apple info, and money amounts (total, tax, shipping). Once user clicks OK, I want the apple purchase to commence.

I was able to implement the single form way by passing in the request.user object in the kwargs. However, with the FormWizard, I cannot figure this out.

Am I approaching the problem wrong and is the FormWizard not the proper way to do this? If it is, how can the Form __init__ method access the user object from the HTTP request?

A: 

I don't know if answering one's own question is an acceptable behaviour on StackOverflow, here is my solution to my own problem.

First, ditch FormWizard.

I have one form. Two views: buy_apples and buy_apples_confirm

First view only handles GET. It prints out the unbound form, with an action to go to the URL of the second view.

The second view checks for the presence of a POST parameter named "confirm". If it is not present (as it is not when the view is loaded the first time) it:

  1. Adjusts the widget on all the fields to be HiddenInput
  2. Writes out template which gives an order summary. This template also sets a hidden field called "confirm" to 1 (even though this field does not exist on the Form)

When the user clicks to buy the apples, the form is submitted back and the buy_apples_confirm view is invoked one more time. This time, a POST parameter called "confirm" is present, so we actually process the purchase transaction and the user gets his apples.

I welcome any critiques on this method or better ways of handling the situation. I am new to Django and find that there are many different ways of approaching a problem. I want to learn from the best though.

+4  A: 

I haven't used it, but for the situation you describe, it seems like you may want to try the FormPreview instead of the FormWizard. From the documentation it sounds like what you're after.

Tom
Interesting. Thanks for pointing that out, Tom.
ayaz
Thank you for pointing out FormPreview. However, in my case, part of the problem is passing in an additional **kwargs value (request.user as 'user') to the __init__ of the Form constructor (needed for dynamic form generation), and I do not see how that is possible with the FormPreview.
I'm not sure whether it'd help (with FormPreview), but something you could do in your code above is not to put that code in the __init__ method. e.g. x = BuyAppleForm(), then do x.set_choices_for_user(request.user) before it's displayed.
Tom
A: 

Thank you krys for answering to your own question. Helped me, but I still got some remarks.

FormPreview is not the way to go since it as far as I know does not support dynamic forms. It relies on a fixed form class to generate the from from there. But we are generating dynamically here with a function. Maybe FormPreview will support this one day (or already does and I dont know how).

Krys solution seems to do the same as FormPreview. Only the hash is left out, so user may change data in the hidden fields or do you check it again?. If you check it again, that would not be following DRY because you duplicate the check (okay, could be in a reusable method, so only tiny repetition).

What I was wondering, how do you adjust the widget? Do you duplicate the form with the new widgets or is there a way to change that dynamically?

A: 

How about changing the call method to take an extra parameter?

something similar to this: http://d-w.me/blog/2010/3/18/15/

hendrixski