如何添加<div>标签而不是 <li> [英] how to add <div> tag instead of <li>

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

问题描述

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初始值 = []type_selection = Types.objects.filter(parent_type_id=type_id,is_active=True)对于 type_selection 中的 type_selection:initial_val.append(type_selection.id)_type_checkbox.initial = initial_val

views.py

def 类型(方法):"""""""""""""类型 = TypeSelectionForm(type_id)返回渲染(请求,'types.html',{'types':types})

在模板中,我像这样渲染字段,

types.html

 {% for field in types.checkbox_field %}<div class="deletelist">{{field}}<br/>

{% 结束为 %}

它正在生成这样的 html,

    <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"/>物理实验室</label></li><li><label for="id_checkbox_field_2"><input checked="checked" type="checkbox" name="checkbox_field" value="599" id="id_checkbox_field_2"/>化学实验室</label></li>

我想用

替换
  • 标签代码>

    需要帮助.

    解决方案

    为什么不使用 Django 模板标签的强大功能?

    from django 导入模板从 django.utils.safestring 导入 mark_safe注册 = 模板.图书馆()@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")返回标记安全(form_as_div)

    把它放在一个模板标签中,然后在你的模板中简单地做到这一点

    {% load ad_div %}{# 一些代码 #}{{ form|as_div }}{# 一些其他代码 #}

    ==============================

    其他方法(更好的清洁器)

    另一种方法是扩展 django 表单模型

    如下

    from django.forms.forms import BaseForm类 AsDiv(BaseForm):def as_div(self):返回 self._html_output(normal_row = u'<div%(html_class_attr)s>%(errors)s%(label)s %(field)s%(help_text)s</div>',error_row = u'

    %s

    ',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 }} 
    

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆