English 中文(简体)
django 1.3 wizard change form_list to change next steps
原标题:django 1.3 wizard alter form_list to change next steps
  • 时间:2011-09-18 18:24:16
  •  标签:
  • django

我担心的是,它包含的是表格A、表格B、表格C和表格D。 我怎样做这样的事情?

目 录

问题回答

形成一种观点,有条件地检查目前提交进程哪一步,然后确认形式,并选择下一个形式。

<>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在会议处理中建造的版本,从第一种形式中挽救任何有效形式领域来克服:





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...