jeudi 29 juillet 2021

Django DetailView - display boolean from models as checkboxes

There's a number of snippets to display booleans in Django Forms as checkboxes (i.e. specifying Checkbox as a widget). For instance (assuming a boolean field defined in the model for bar):

class FooForm(forms.ModelForm):

    class Meta:
        model = Foo
        fields = ['bar'] 
        widgets = {
            'bar' : CheckboxInput(attrs={'class': 'required checkbox form-control'}),   
        }

However I also need to display a (disabled) checkbox in DetailView (client says so). But I can't figure out an elegant way to do this, since I don't have a form meta for details view...

My current thinking is something like this (bootstrap checkbox):

<div class="form-check">
   <label class="form-check-label">
      <input type="checkbox"  disabled>Bar
   </label>
<\div>

Any way to accomplish this in a fashion closer to the Form's widgets?




Aucun commentaire:

Enregistrer un commentaire