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