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?
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:
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.