__init__
python ( ) 関数の仕組みを理解するのに少し苦労しています。私がやろうとしているのは、django で新しいフォームを作成し、uni_form ヘルパーを使用して、フィールドセットを使用してカスタムの方法でフォームを表示することですが、フォームのレイアウトをわずかに変更する引数をフォームに渡しています。そして、私はこれを機能させる方法を理解できません。これが私のコードです:
class MyForm(forms.Form):
name = forms.CharField(label=_("Your name"), max_length=100, widget=forms.TextInput())
city = forms.CharField(label=_("Your city"), max_length=100, widget=forms.TextInput())
postal_code = forms.CharField(label=_("Postal code"), max_length=7, widget=forms.TextInput(), required=False)
def __init__(self, city, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
if city == "Vancouver":
self.canada = True
if self.canada:
# create an extra uni_form fieldset that shows the postal code field
else:
# create the form without the postal code field
しかし、これが私にとってうまくいかない理由。self. canada は 以外の値を持っているようには見えない__init__
ため、その引数を関数に渡したにもかかわらず、クラスで値を使用できません。__init__
これは、self.fields を使用してフォーム全体を内部で作成するという回避策を見つけましたが、これは醜いです。以外で self.canada を使用するにはどうすればよい__init__
ですか?