django 1.3向导alter form_list来更改后续步骤 [英] django 1.3 wizard alter form_list to change next steps

查看:124
本文介绍了django 1.3向导alter form_list来更改后续步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含formA,formB,formC和formD的向导。如果FormA属性名不为空,我想跳过FormB。我如何做这样的事情?



敬意,

解决方案

创建一个视图,有条件地检查当前正在提交的进程的哪个步骤,然后验证表单,并选择下一个表单。



forms.py

  BaseForm(forms.Form)
#我们所有的表单都将有一个隐藏的字段,将它们标识为A,B或C
type = forms.HiddenField(...)

class formA(BaseForm)
#这些是你的其他属性(如'name'等)
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 /表单向导中调用,并且将弄清楚它正在接收的三种形式中的哪一种以及将为下一步提供的形式。当所有数据都被收集时,可以执行一些重定向或进一步的逻辑。

  def form_wizard(self,request):
next_form = None
curr_form = None
如果request.method =='POST':
#了解我们是否在链中收到了一个表单
type = request .pOST.get(type,None)
如果type =='a':
#我们现在正在处理Form A
curr_form = FormA(request.POST)
如果curr_form.is_valid():
#对属性进行检查(即name == None)
如果curr_form.cleaned_data.get('a_1',无):
next_form = FormB ()
#现在为下一个表单设置类型为b的值
next_form.fields ['type']。initial ='b'
else:
next_form = FormC()
next_form.fields ['type']。initial ='c
elif type =='b':
#处理B
curr_form = FormB(request.POST)
如果form.is_valid():
#这个表单
....
next_form = FormC()
next_form.fields ['type']。initial ='c'
elif type =='c':
#处理C
curr_form = FormC(request.POST)
如果curr_form.is_valid():
#链中的最后一个表单;重定向或做别的东西
return HttpResponseRedirect(...)
else:
#首先访问向导,给他们第一个表单
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 =submitvalue =Submit/>
< / form>
...

您可能面临的唯一其他问题是如何保留数据第一个形式,直到你成功完成第三个表格。通过使用Django的内置会话处理,在用户会话中从第一个表单中保存任何有效的表单字段可以很容易地克服:



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


I have a wizard that contain formA, formB, formC and formD. I want to skip FormB if FormA attribute name is not null. How could I do such a thing?

regards,

解决方案

Create a view that will conditionally check which step of the process is currently being submitted then validate the form and also choose what form comes next.

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

This view could be called from the URL /form-wizard/ and will figure out which of the three forms it is receiving and which form it will provide for the next step. When all the data has been gather, some redirect or further logic can be performed.

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}

Finally, in your template:

template.html

This will render whichever form we have passed to the template (A,B or C)

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

The only other problem you might face is how to keep data from the first form until you have succesfully completed the third form. This can be easily overcome by saving any valid form fields from the first form in the users session using Django's built in session handling:

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

这篇关于django 1.3向导alter form_list来更改后续步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆