Django FormWizard with dynamic forms

后端 未结 5 1911
既然无缘
既然无缘 2021-02-04 18:48

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

class BuyAppleForm(forms.Form):
   creditcard = forms.C         


        
5条回答
  •  天涯浪人
    2021-02-04 19:29

    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.

提交回复
热议问题