【问题标题】:Django python change multipleChoiceField html outputDjango python更改multipleChoiceField html输出
【发布时间】:2017-04-02 22:38:18
【问题描述】:

我有多个ChicesField:

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

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

现在它会生成以下 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>

如何更改我的代码以生成类似于以下内容的 html 结构:

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

【问题讨论】:

    标签: python django


    【解决方案1】:

    您可以手动呈现表单,然后使用您喜欢的任何标记

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

    请参阅此示例以循环遍历每个表单字段:

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

    【讨论】:

      猜你喜欢
      • 2013-10-02
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 2011-01-10
      • 2020-06-29
      • 2010-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多