以下是我定义的Django Form Field。
source_currency = forms.CharField(max_length=5)
以下是选择/选项HTML
<select name="fancySelect" for="{{ form.source_currency.id_for_label }}" class="makeMeFancy" id="drop1">
<!-- Notice the HTML5 data attributes -->
<option value="BTC" selected="selected" data-skip="1" data-icon="assets/images/large/bitcoin.png" data-html-text="BTC<i>">BTC<span class="select_coin_button_arrow">▾</span></option>
<option value="BTC" data-icon="assets/images/small/bitcoin.png" data-html-text="BTC<i>" >BTC</option>
<option value="ETH" data-icon="assets/images/small/ether.png" data-html-text="ETH<i>">ETH</option>
如何链接它们以便在提交时这些值以字符串形式传递?
使用ChoiceField
而不是CharField
,它会自动将所选值作为字符串传递。
source_currency = forms.ChoiceField(
label=_("Source Currency"),
help_text=_("..."),
widget=forms.Select(attrs={'class': 'makeMeFancy'}),
choices=((1, _("BTC")), (2, _("ETH")),),
initial=1
)
UPDATE
或者,如果要将现有集成与后端连接。
注意:select标签的
name
属性的值应该是source_currency
而不是fancySelect