カスタムフィールドとカスタムウィジェットを使用して私の目標を達成しようとしている、カスタマイズされた問題で何かを構築しようとしています。しかし、残念ながら、私をガイドするのに十分なドキュメントが見つかりません...
私の場合、ウィザードの最初のフォームを構築しようとしています。最初のフォームは、データベースから事前入力された選択肢辞書を渡す単一のフィールドで構成されます。辞書は次のようになります
choices = {
1 : {
'display_name' : 'Bronze',
'description' : 'this package contain all the cool features and much more.',
},
2 : {
'display_name' : 'Silver',
'description' : 'this package contain all the cool features and much less.',
},
}
表示されているディクショナリには、初期化時にフィールドに供給されるデータベースから動的に提供されるサービスが含まれています。一方、次の HTML を設定するカスタム フィールドとウィジェットを作成する必要があります。
<div class="choices">
<div class="span3">
<h2>Bronze</h2>
<p>
this package contain all the cool features and much more.
</p>
<input type="button" name="Bronze" value="1" />
</div>
<div class="span3">
<h2>Silver</h2>
<p>
this package contain all the cool features and much less.
</p>
<input type="button" name="Silver" value="2" />
</div>
</div>
ここで、各 span3 は、パッケージの説明名と、選択してウィザードの次のステップに移動するためのボタンを含む行を表します。一方、次のように簡単なカスタムフィールドを構築しようとしました
class PackageField(forms.CharField):
default_error_messages = {
'not_valid_package': _(u'Package selected is not a valid package.'),
}
def to_python(self, value):
print value
if value in self.validators:
return None
return value
そしてwidget.py
class PackageWidget(MultiWidget):
_choices = ()
def __init__(self, choices, attrs=None):
self._choices = choices
super(PackageWidget, self).__init__(attrs)
def render(self, name, value, attrs=None):
output = []
for k, v in self._choices:
output.append('<div class="span3">%s<input type="button" class="btn"/></div>' % v)
return mark_safe(self.format_output(output))
しかし、エラーをスローしているため、カスタムフィールドとウィジェットの書き方に多くの間違いがあると確信しています.
正しいカスタム フィールドとウィジェットを作成する方法について、誰かが私を正しい方向に導くことができれば幸いです。