3

国を選択できる住所フォームがあります。フォームが読み込まれる前に値を設定したい。どうすればそれができますか?フォームは次のとおりです。

from django import forms
from django.utils.translation import gettext as _

from django_countries import countries
from core.api import NcpAPI


class AddressForm(forms.Form):
    # first_name        = forms.CharField(label=_("First Name"), widget=forms.TextInput(attrs={'class':'form-text required'}))
    # last_name         = forms.CharField(label=_("Last Name"), widget=forms.TextInput(attrs={'class':'form-text required'}))
    # company       = forms.CharField(label=_("Company"), widget=forms.TextInput(attrs={'class':'form-text required'}))
    street          = forms.CharField(label=_("Street/PoBox"), widget=forms.TextInput(attrs={'class':'form-text required'}))
    address1        = forms.CharField(label=_("Address1"), widget=forms.TextInput(attrs={'class':'form-text required'}))
    address2        = forms.CharField(required=False, label=_("Address2"), widget=forms.TextInput(attrs={'class':'form-text'}))
    address3        = forms.CharField(required=False, label=_("Address3"), widget=forms.TextInput(attrs={'class':'form-text'}))
    city            = forms.CharField(label=_("City"), widget=forms.TextInput(attrs={'class':'form-text required'}))
    postal_code     = forms.CharField(label=_("Postal code"), widget=forms.TextInput(attrs={'class':'form-text required'}))
    country         = forms.ChoiceField(label=_("Country"), choices=countries.COUNTRIES, widget=forms.Select())
4

1 に答える 1

2
country = forms.ChoiceField(label=_("Country"), 
          choices=countries.COUNTRIES, widget=forms.Select(), initial= 'us')

または、フォームコンストラクターを呼び出すときに開始できます

form = AddressForm(initial={'country': 'us'})
于 2013-08-07T07:37:27.727 に答える