【问题标题】:how to produce <span> instead of <li> tag如何生成 <span> 而不是 <li> 标签
【发布时间】:2013-09-06 16:43:57
【问题描述】:

forms.py

class ZergitForm(forms.Form):

    zerg_selection = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(),required=False)

views.py

def feeds(request):

    if request.method == 'POST':
        zergform = ZergitForm(request.POST)
        """"""""
        some other logic comes here
        """"""""
    selection = ZergitForm(zerg_id)
    return render(request, 'feed_list.html',{'zerg_selection':zerg_selection})

feed_list.html

{% for feed in selection.zerg_selection %}
<span class="checkbox_select">{{ feed }}</span>
{% endfor %} 

问题是它呈现为&lt;li&gt; 标记中的复选框列表。但我需要的是&lt;span&gt; 标记内带有复选框的输入字段。如何在我的应用程序中实现它。

【问题讨论】:

    标签: python django django-models django-forms django-admin


    【解决方案1】:

    对此没有干净的解决方案,要么您使用覆盖的渲染方法定义自己的 CheckboxSelectMultiple 小部件(恕我直言,这会带来很多开销):

    https://github.com/django/django/blob/1.5.2/django/forms/widgets.py#L754

    或者你使用小技巧(在这种情况下你替换 &lt;li&gt;&lt;/li&gt; 等):

    Django Setting class of <ul> instead of <input> with CheckboxSelectMultiple widget

    或者你自定义模板输出:

    https://stackoverflow.com/a/15441506/1566605

    【讨论】:

      猜你喜欢
      • 2013-09-06
      • 2021-11-13
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多