how to add
tag instead of
  • 前端 未结 4 1839
    [愿得一人]
    [愿得一人] 2020-12-18 05:35

    forms.py

    class TypeSelectionForm(forms.Form):
        checkbox_field = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(), label=\"\", required=Fals         
    
    
            
    4条回答
    •  青春惊慌失措
      2020-12-18 06:22

      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("

      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'%(errors)s%(label)s %(field)s%(help_text)s
      ', error_row = u'
      %s
      ', row_ender = '
    ', help_text_html = u' %s', errors_on_separate_row = False)

    Then you could just do this is your template

    {{ form.as_div }} 
    

    提交回复
    热议问题