3 回答

TA貢獻2037條經(jīng)驗 獲得超6個贊
您好,很高興為您解答。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | from django.utils.translation import ugettext_lazy as _ from django import forms from django.forms.formsets import BaseFormSet from django.forms.fields import FileField from django.forms.util import ValidationError
from django.shortcuts import render_to_response from django.contrib.formtools.wizard import FormWizard
from ddtcms.office.equipment.models import Equipment,Characteristic,CharacteristicValue
class EquipmentForm(forms.ModelForm):
class Meta: model = Equipment
class CharacteristicValueForm(forms.Form): def clean(self): a=self.fields s=self.data self.cleaned_data = {} # 下面的這一段for 是從 django的forms.py中的 full_clean 中復(fù)制來的 for name, field in self.fields.items(): # value_from_datadict() gets the data from the data dictionaries. # Each widget type knows how to retrieve its own data, because some # widgets split data over several HTML fields. value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) try: if isinstance(field, FileField): initial = self.initial.get(name, field.initial) value = field.clean(value, initial) else: value = field.clean(value) self.cleaned_data[name] = value if hasattr(self, 'clean_%s' % name): value = getattr(self, 'clean_%s' % name)() self.cleaned_data[name] = value except ValidationError, e: self._errors[name] = self.error_class(e.messages) if name in self.cleaned_data: del self.cleaned_data[name] #cl=self.cleaned_data #debug()<<<調(diào)試用的,查看cl的值,主要是看self.cleaned_data的值,如果return了,就看不到了 return self.cleaned_data
class EquipmentCreateWizard(FormWizard): def done(self, request, form_list): return render_to_response('equipment/done.html', { 'form_data': [form.cleaned_data for form in form_list], })
def get_form(self, step, data=None): "Helper method that returns the Form instance for the given step." form = self.form_list[step](data, prefix=self.prefix_for_step(step), initial=self.initial.get(step, None))
if step == 1: if data: cg = data.get('0-category', 1) cs = Characteristic.objects.all().filter(category__id=cg) for c in cs: form.fields['Characteristic-'+str(c.id)] = forms.CharField(label = c.name) g=form.fields #debug() return form
# 從wizard.py中復(fù)制過來進行更改的. def render(self, form, request, step, context=None): "Renders the given Form object, returning an HttpResponse." old_data = request.POST prev_fields = [] if old_data: hidden = forms.HiddenInput() # Collect all data from previous steps and render it as HTML hidden fields. for i in range(step): old_form = self.get_form(i, old_data) hash_name = 'hash_%s' % i prev_fields.extend([bf.as_hidden() for bf in old_form]) prev_fields.append(hidden.render(hash_name, old_data.get(hash_name, self.security_hash(request, old_form)))) if step == 1: cg = old_data.get('0-category', 1) cs = Characteristic.objects.all().filter(category__id=cg) for c in cs: form.fields['Characteristic-'+str(c.id)] = forms.CharField(label = c.name) g=form.fields #debug() if step == 2: debug() return super(EquipmentCreateWizard, self).render(form, request, step, context=None)
def get_template(self, step): return 'equipment/wizard_%s.html' % step |
EquipmentCreateWizard其實也可以放在views.py中,而且我覺得更合理一點.在EquipmentCreateWizard 中,我試著修改過process_step 函數(shù),但是得不到正確的結(jié)果,后來修改了get_form,都是想從django的formtools的wizard.py中復(fù)制過來再進行修改的.get_form的修改也沒有得到正確的結(jié)果.后來就修改render函數(shù),在第2步的時候,我將動態(tài)參數(shù)個數(shù)顯示出來了.但是到最后結(jié)束done的環(huán)節(jié),取得的formdata中,第二個form沒有數(shù)據(jù),就是一個空的{},于是我又重新修改get_form函數(shù),無非就是判斷是不是第二步,然后給第二個form動態(tài)添加幾個field:
1 2 3 4 5 6 7 | if step == 1: cg = old_data.get('0-category', 1) cs = Characteristic.objects.all().filter(category__id=cg) for c in cs: form.fields['Characteristic-'+str(c.id)] = forms.CharField(label = c.name) g=form.fields #debug() |
這段代碼在get_form和 render中都有,都是判斷是不是第2步,然后就根據(jù)第1步中選擇的設(shè)備的分類來查詢到具體的分類,再根據(jù)分類來獲取該種分類的設(shè)備有哪些參數(shù),然后根據(jù)參數(shù)個數(shù)修改form的參數(shù)field的個數(shù).'Characteristic-'+str(c.id)是用來以后保存數(shù)據(jù)的時候,split這個字符串,得到參數(shù)的id,并在參數(shù)值表中保存Characteristic-1,Characteristic-2...的value.
g=form.fields#debug()
用來斷點查看參數(shù)field有多少個,是否修改成功.
=========================
1 2 3 4 5 6 7 8 9 | from django.conf.urls.defaults import * from ddtcms.office.equipment.forms import EquipmentForm,CharacteristicValueForm,EquipmentCreateWizard
urlpatterns = patterns('ddtcms.office.equipment.views', url(r'^$', 'index', name="equipment_index"), url(r'^add/$', 'equipment_create', name="equipment_create"), url(r'^add-by-wizard/$',EquipmentCreateWizard([EquipmentForm, CharacteristicValueForm]), name="equipment_create_by_wizard"), ) 以上代碼,csdnbolg 自動過濾了 $符號,我加了上去,可能有不對的地方. |
==========================wizard_0.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | {% block content %}
<h2>添加/修改設(shè)備向?qū)?lt;/h2> <p>第 {{ step }} 步, 共 {{ step_count }} 步.</p> <p>填寫設(shè)備基本情況</p>
<form method="POST" action="">{% csrf_token %} <table> {{ form }} </table> <input type="hidden" name="{{ step_field }}" value="{{ step0 }}" /> {{ previous_fields|safe }} <input type="submit" value="Submit" /> </form>
{% endblock %} |
===================wizard_1.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | {% block content %}
<h2>添加/修改設(shè)備向?qū)?lt;/h2> <p>第 {{ step }} 步, 共 {{ step_count }} 步.</p> <p>填寫設(shè)備參數(shù), 如果沒有要填寫的內(nèi)容, 請直接點擊確定.</p>
<form method="POST" action="">{% csrf_token %} <table> {{ form }} </table> <input type="hidden" name="{{ step_field }}" value="{{ step0 }}" /> {{ previous_fields|safe }} <input type="submit" value="Submit" /> </form>
{% endblock %} |
====================done.html
1 2 3 4 5 6 7 8 | {% block content %}
<h2>添加/修改設(shè)備向?qū)?lt;/h2> <p>您已經(jīng)成功添加了一個設(shè)備.</p>
{{form_data}}
{% endblock %} |
============
還可以用另外的form來實現(xiàn)formwizard,就是第一個form1,主要用來讓用戶選擇設(shè)備的分類,form2就根據(jù)前面的來動態(tài)生成參數(shù)的表單.原理是一樣的.
還有就是寫2個view來模擬formwizard,第一個view增加一個設(shè)備,第二個view帶設(shè)備id這個參數(shù)即可,可以很有效的增加設(shè)備的參數(shù).

TA貢獻1836條經(jīng)驗 獲得超13個贊
怎么做到使用django動態(tài)定義表單
啟動Python腳本
Notepad++ Python腳本需要放置到特殊的目錄中,以便于通過Python插件識別,然后可以在Notepad++中啟動.通常這個目錄是%APPDATA%\Notepad++\plugins\config\PythonScript。這個腳本也可以通過菜單Plugins->Python Script->Scripts啟動.我們也可以創(chuàng)建工具欄按鈕為這些腳本以便于快速啟動它們。
編程對統(tǒng)計字?jǐn)?shù)
為了展示這個插件,寫一個python 腳本 ,用來計算 字符,字,行在當(dāng)前的Notepad++編輯窗口。
- 3 回答
- 0 關(guān)注
- 521 瀏覽
添加回答
舉報