形式獲取所選值
class AddressForm(forms.Form):
Country = forms.ModelChoiceField(queryset=Country.objects.filter(Enabled=True), empty_label="Select Country", widget=forms.Select(attrs={'class':'form-control'}))
HTML
<form method="POST" action="">
{% csrf_token %}
.
.
<div class="form-group col-md-4">
{{ addressForm.Country }}
</div>
</form>
視圖
addressForm = AddressForm(request.POST, prefix="AddressForm")
country = addressForm['Country']
但該國變量包含整個HTML元素,而不是所選擇的值。如forms.CharField
如何從窗體元素中檢索選定的值?
你爲什麼要訪問它?這是後驗證? – Sayse
我需要將數據從AddressForm傳遞給模型Address對象以調用.save方法(?) – John