CheckboxSelectMultiple

CheckboxSelectMultiple

本文介绍了如何生成< span>而不是< li>标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

feed_list.html

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

问题是将渲染列为复选框列表一个< li> 标签。但是我需要的是输入字段与< span> 标签。如何在我的应用程序中实现它。

Problem is it is rendering as a list of check boxes in a <li> tag.But what i required is the input field with check box inside the <span> tag.How to implement it in my application.

推荐答案

没有干净的解决方案,您可以使用覆盖的render方法定义自己的CheckboxSelectMultiple小部件很多开销):

There is no clean solution for that, either you define your own CheckboxSelectMultiple widget with overridden render method (which imho, is way to much overhead):

或您使用小黑客(在这种情况下,您将替换< li> < / li> 等):

or you use small hack (in that case you replace <li> and </li> etc):

或自定义模板输出:

这篇关于如何生成&lt; span&gt;而不是&lt; li&gt;标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:08