简体   繁体   中英

Django python change multipleChoiceField html output

I have multipleChicesField:

OPTIONS = (
        ("AUT", "Austria"),
        ("DEU", "Germany"),
        ("NLD", "Neitherlands"),
    )

    countries = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(attrs={"name": "select_0","class": "fff"}),
                                          choices=OPTIONS)

Right now it produces following html:

    <ul id="id_countries">
      <li><label for="id_countries_0"><input class="fff" id="id_countries_0" name="countries" type="checkbox" value="AUT"> Austria</label></li>

      <li><label for="id_countries_1"><input class="fff" id="id_countries_1" name="countries" type="checkbox" value="DEU"> Germany</label></li>

      <li><label for="id_countries_2"><input class="fff" id="id_countries_2" name="countries" type="checkbox" value="NLD"> Neitherlands</label></li>
    </ul>

How do I change my code to produce html structure something like:

  <div class="someClass">
    <input class="fff" id="id_countries_0" name="countries" type="checkbox" value="AUT"> 
    <label for="id_countries_0">Austria</label>
  </div>

  <div class="someClass">
    <input class="fff" id="id_countries_1" name="countries" type="checkbox" value="DE"> 
    <label for="id_countries_0">Germany</label>
  </div>

You can render the form manually, and then use whatever markup you like

https://docs.djangoproject.com/en/1.10/topics/forms/#rendering-fields-manually

See this example for looping through each form field:

https://docs.djangoproject.com/en/1.10/topics/forms/#looping-over-the-form-s-fields

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM