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

    forms.py

    class TypeSelectionForm(forms.Form):
        checkbox_field = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(), label=\"\", required=Fals         
    
    
            
    4条回答
    •  猫巷女王i
      2020-12-18 06:17

      From the documentation:

      New in Django 1.4.

      For more granular control over the generated markup, you can loop over the radio buttons in the template. Assuming a form myform with a field beatles that uses a RadioSelect as its widget:

      {% for radio in myform.beatles %}
      
      {{ radio }}
      {% endfor %}

      In your template, you should have this:

      {% for radio in types.checkbox_field %}
         {{ radio }}
      {% endfor %}
      

      You should also use a ModelMultipleChoiceField:

      class TypeSelectionForm(forms.Form):
          checkbox_field = forms.ModelMultipleChoiceField(label="",
                                                          queryset=Types.objects.none(),
                                                          required=False)
      
          def __init__(self, *args, **kwargs):
              qs = kwargs.pop('queryset')
              super(TypeSelectionForm, self).__init__(*args, **kwargs)
              self.fields['checkbox_field'].queryset = qs
      

      Initiate it like this from your view:

      def types(method):
          """"""""""""
          qs = Types.objects.filter(parent_type_id=type_id,is_active=True)
          types = TypeSelectionForm(queryset=qs)
          return render(request,'types.html',{'types':'types'})
      

    提交回复
    热议问题