how to produce <span> instead of <li> tag

跟風遠走 提交于 2019-12-24 06:38:09

问题


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

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.


回答1:


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

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

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

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

or you customize template output:

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



来源:https://stackoverflow.com/questions/18662626/how-to-produce-span-instead-of-li-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!