簡體   English   中英

django 1.3向導更改form_list以更改下一步

[英]django 1.3 wizard alter form_list to change next steps

我有一個包含formA,formB,formC和formD的向導。 如果FormA屬性名稱不為null,我想跳過FormB。 我該怎么做?

問候,

創建一個視圖,該視圖將有條件地檢查當前正在提交流程的哪個步驟,然后驗證表單並選擇下一個表單。

forms.py

class BaseForm(forms.Form)
    # All our forms will have a hidden field identifying them as either A, B or C
    type = forms.HiddenField(...)

class formA(BaseForm)
    # These are the rest of your attibutes (such as 'name' etc.)
    a_1 = forms.TextField(...)
    a_2 = forms.TextField(...)

class formB(BaseForm)
    b_1 = forms.TextField(...)
    b_2 = forms.TextField(...)
    ....

class formC(BaseForm)
    c_1 = forms.TextField(...)
    c_1 = forms.TextField(...)

views.py

可以從URL / form-wizard /調用此視圖,並將確定要接收的三種表單中的哪一種,以及將為下一步提供的表單。 收集完所有數據后,可以執行一些重定向或其他邏輯。

def form_wizard(self, request):
    next_form = None
    curr_form = None
    if request.method=='POST':
        # Find out if we have received a form in our chain
        type = request.POST.get("type", None)
        if type == 'a':
            # We are now processing Form A
            curr_form = FormA(request.POST)
            if curr_form.is_valid():
                # Do a check on the attributes (i.e. name==None)
                if curr_form.cleaned_data.get('a_1',None):
                    next_form = FormB()
                    # Now set the value of type to 'b' for the next form
                    next_form.fields['type'].initial = 'b'
                else:
                    next_form = FormC()
                    next_form.fields['type'].initial = 'c'
        elif type == 'b':
            # Processing B
            curr_form = FormB(request.POST)
            if form.is_valid():
                # Do something with this form
                .... 
                next_form = FormC()
                next_form.fields['type'].initial = 'c'
        elif type == 'c':
             # Processing C
             curr_form = FormC(request.POST)
             if curr_form.is_valid():
             # Last form in chain; either redirect or do something else
             return HttpResponseRedirect(...)
        else:
            # First visit to the wizard, give them the first form
            curr_form = FormA()
            curr_form.fields['type'].initial = 'b'

    return .... {'form':curr_form}

最后,在您的模板中:

template.html

這將呈現我們傳遞給模板的任何形式(A,B或C)

...
<form action="/form-wizard/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
...

您可能面臨的唯一另一個問題是如何在成功完成第三個表格之前保留第一個表格中的數據。 通過使用Django的內置會話處理功能,可以從用戶會話中的第一個表單中保存任何有效的表單字段,從而輕松解決這一問題:

https://docs.djangoproject.com/en/dev/topics/http/sessions/#examples

django.contrib.formtools.wizard應用已針對Django 1.4進行了重寫。 新應用已被反向移植為django-formwizard ,可用於Django 1.3。

如果您要編寫新的formwizard視圖,建議您使用django-formwizard應用程序。 不推薦使用舊的formwizard代碼時,這將使遷移到Django的未來版本更加容易。

Django文檔中有一個針對Django開發版本的部分,描述了如何跳過 formwizard的特定步驟

如果您堅持使用1.3,則FormWizard.process_step是您要尋找的掛鈎。

在文檔中,您可以使用此方法執行以下操作:

根據先前提交的表單動態更改self.form_list。

一個簡單的實現是:

def process_step(self, request, form, step):
  if step == 1 && form.cleaned_data['name'] is not None:
    self.form_list.pop(2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM