我在HTML / CSS中修复了选择选项列表。我想把它链接到django char field.But没找到方法

问题描述 投票:0回答:1

以下是我定义的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&lt;i&gt">BTC<span class="select_coin_button_arrow">▾</span></option>
        <option value="BTC" data-icon="assets/images/small/bitcoin.png" data-html-text="BTC&lt;i&gt" >BTC</option>
        <option value="ETH" data-icon="assets/images/small/ether.png" data-html-text="ETH&lt;i&gt;">ETH</option>

如何链接它们以便在提交时这些值以字符串形式传递?

python html django
1个回答
0
投票

使用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

© www.soinside.com 2019 - 2024. All rights reserved.