我担心的是,它包含的是表格A、表格B、表格C和表格D。 我怎样做这样的事情?
目 录
我担心的是,它包含的是表格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在会议处理中建造的版本,从第一种形式中挽救任何有效形式领域来克服:
django.contrib.formtools.wizard
对Django1.4进行了改装。 新的数据已作为django-formwizard,可供Django 1.3使用。
如果你撰写新的表格,我建议你使用django-formwizard<>。 这将使较容易地迁移到未来的Django版本,因为旧的表格维扎代码被删除。
如果您坚持1.3,FormWizard.process_ped_dle
<>>> 这正是你们的looking。
引言:
动态改变自我。 表格_清单,根据以前提交的表格。
简单执行是:
def process_step(self, request, form, step):
if step == 1 && form.cleaned_data[ name ] is not None:
self.form_list.pop(2)
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.
I want to email users an encrypted file from a Django Backend Script (running through manage.py) on Ubuntu. How can I encrypt the data so that it is unreadable by 3rd parties who might intercept or ...
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....
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 ...
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 ...
I m trying to reset a database in Django, using: python manage.py reset app but get the following error: Error: Error: app couldn t be reset. Possible reasons: * The database isn t running or isn ...
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: &...
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 ...