[Python] Django: How can I create a multiple select form?

Discussão em 'Python' iniciado por Stack, Agosto 29, 2025.

  1. Stack

    Stack Membro Participativo

    I'm beginner in Django/Python and I need to create a multiple select form. I know it's easy but I can't find any example. I know how to create a CharField with a widget but I get confused of all the options inside fields.py.

    For example I don't know which one of the followings is best for a multiple select form.

    'ChoiceField', 'MultipleChoiceField',
    'ComboField', 'MultiValueField',
    'TypedChoiceField', 'TypedMultipleChoiceField'


    And here is the form I need to create.

    <form action="" method="post" accept-charset="utf-8">
    <select name="countries" id="countries" class="multiselect" multiple="multiple">
    <option value="AUT" selected="selected">Austria</option>
    <option value="DEU" selected="selected">Germany</option>
    <option value="NLD" selected="selected">Netherlands</option>
    <option value="USA">United States</option>
    </select>
    <p><input type="submit" value="Continue &rarr;"></p>
    </form>


    EDIT:

    One more small question. If I want to add to each option one more attribute like data:

    <option value="AUT" selected="selected" data-index=1>Austria</option>


    How can I do it?

    Thanks for any help!

    Continue reading...

Compartilhe esta Página