如何添加< div>标记,而不是< li> [英] how to add <div> tag instead of <li>
问题描述
forms.py
类TypeSelectionForm(forms.Form):
checkbox_field = forms.MultipleChoiceField(widget = form.CheckboxSelectMultiple(),label =,required = False)
def __init __(self,type_id,* args,** kwargs):
super(TypeSelectionForm,self) (* args,** kwargs)
_type_checkbox = self.fields ['checkbox_field']
MY_CHOICES =((type.id,type.title)用于类型)
_type_checkbox.choices = MY_CHOICES
initial_val = []
type_selection = Types.objects.filter(parent_type_id = type_id,is_active = True)
for type_selection in type_selection:
initial_val.append (type_selection.id)
_type_checkbox.initial = initial_val
views.py
def types(method):
types = TypeSelectionForm type_id)
return render(request,'types.html',{'types':types})
在模板中,我正在渲染这样的字段,
types.html
{%for types.checkbox_field%}
< div class =deletelist>
{{field}}< br />
< / div>
{%endfor%}
正在生产这样的html,
< ul>
< li>< label for =id_checkbox_field_0>< input checked =checkedtype =checkboxname =checkbox_fieldvalue =597id =id_checkbox_field_0/> comp lab< / label>< / li>
< li>< label for =id_checkbox_field_1>< input checked =checkedtype =checkboxname =checkbox_fieldvalue =598id =id_checkbox_field_1/> phy lab< / label>< / li>
< li>< label for =id_checkbox_field_2>< input checked =checkedtype =checkboxname =checkbox_fieldvalue =599id =id_checkbox_field_2/>化学实验室< / label>< / li>
< / ul>
我要替换< ul>
和< li>
标签与< div class =class-name>
需要帮助。
为什么不使用Django模板标签的力量? / p>
从django导入模板
从django.utils.safestring import mark_safe
register = template.Library()
@ register.filter(as_div)
def as_div(form):
form_as_div = form.as_ul()。replace(< ul ,< div)替换(< / ul,< / div)
form_as_div = form_as_div.replace(< li,< div < / li,< / div)
return mark_safe(form_as_div)
把它放在一个模板标签中,然后简单地在你的模板中执行
{%load ad_div%}
{#some code#}
{{form | as_div}}
{#其他代码#}
==========================
其他方法(Better Cleaner)
另一种方法是扩展django表单模型
如下
$ b来自django.forms.forms import的$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
return self._html_output(
normal_row = u'< div%(html_class_attr)s>%(errors)s%(label)s%(field)s%(help_text)s< / div> ',
error_row = u'< div>%s< / div>',
row_ender ='< / div>',
help_text_html = u'< span class = helptext>%s< / span>',
errors_on_separate_row = False)
然后你可以这样做是你的模板
{{form.as_div}}
forms.py
class TypeSelectionForm(forms.Form):
checkbox_field = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(), label="", required=False)
def __init__(self, type_id, *args, **kwargs):
super(TypeSelectionForm, self).__init__(*args, **kwargs)
_type_checkbox = self.fields['checkbox_field']
MY_CHOICES=((type.id, type.title) for type in type)
_type_checkbox.choices = MY_CHOICES
initial_val = []
type_selection = Types.objects.filter(parent_type_id=type_id,is_active=True)
for type_selection in type_selection:
initial_val.append(type_selection.id)
_type_checkbox.initial = initial_val
views.py
def types(method):
""""""""""""
types = TypeSelectionForm(type_id)
return render(request,'types.html',{'types':types})
In template I'm rendering the field like this,
types.html
{% for field in types.checkbox_field %}
<div class="deletelist">
{{field}}<br />
</div>
{% endfor %}
It is producing the html like this,
<ul>
<li><label for="id_checkbox_field_0"><input checked="checked" type="checkbox" name="checkbox_field" value="597" id="id_checkbox_field_0" /> comp lab</label></li>
<li><label for="id_checkbox_field_1"><input checked="checked" type="checkbox" name="checkbox_field" value="598" id="id_checkbox_field_1" /> phy lab</label></li>
<li><label for="id_checkbox_field_2"><input checked="checked" type="checkbox" name="checkbox_field" value="599" id="id_checkbox_field_2" /> chem lab</label></li>
</ul>
I want to replace the <ul>
and <li>
tag with <div class="class-name">
Need help.
Why not use the power of Django template tags ?
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@register.filter("as_div")
def as_div(form):
form_as_div = form.as_ul().replace("<ul", "<div").replace("</ul", "</div")
form_as_div = form_as_div.replace("<li", "<div").replace("</li", "</div")
return mark_safe(form_as_div)
Put that in a template tag and then do this simply in your template
{% load ad_div %}
{# some Code #}
{{ form|as_div }}
{# some other code #}
============================
Other approach (Better Cleaner)
Another approach would be to extend django forms model
as follows
from django.forms.forms import BaseForm
Class AsDiv(BaseForm):
def as_div(self):
return self._html_output(
normal_row = u'<div%(html_class_attr)s>%(errors)s%(label)s %(field)s%(help_text)s</div>',
error_row = u'<div>%s</div>',
row_ender = '</div>',
help_text_html = u' <span class="helptext">%s</span>',
errors_on_separate_row = False)
Then you could just do this is your template
{{ form.as_div }}
这篇关于如何添加< div>标记,而不是< li>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!