0

I try to customize my forms fields using django-crispy. But I can make my fields 'readonly'.

Before using django-crispy FormHelper, I used disabled form field attribute and it works well.

Now, I want to use django-cripy helper to render option buttons horizntally but disabled atribute do not works.

I have tried using whidget attribute readonly but neither works.

I also tried https://github.com/django-crispy-forms/django-crispy-forms/issues/47 solution but got an error Could not parse the remainder: ' form.pat readonly True' from 'crispy_field form.pat readonly True'

When I display the template, eli_dat field is correctly disabled (readonly) but not eli_oui_ebo radio buttons...

forms.py

class EligibiliteForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        self.IDE = kwargs.pop("ide", None)
        self.PATIENT = kwargs.pop("patient", None)
        self.USER = kwargs.pop("user", None)
        super(EligibiliteForm,self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-7'
        self.helper.layout = Layout(
            'pat',
            Field(
                AppendedText(
                   'eli_dat',                    
                    mark_safe('<span data-feather="calendar"></span>'),
                )
            ),       
            InlineRadios('eli_oui_ebo'),  
        )

        if self.PATIENT == None:
            FORM_LOCK = Eligibilite.objects.get(ide = self.IDE).ver
        else:
            FORM_LOCK = False # Form can not be locked at creation
        **DISABLED = False**
        INITIAL = None

        if self.PATIENT != None:
            print("form - create")
            FORM_LOCK = False
            **DISABLED = False**
            INITIAL = Patient.objects.get(ide=self.PATIENT.ide)

        elif self.PATIENT == None and not FORM_LOCK:
            print("form - update not locked")
            **DISABLED = False**
            INITIAL = None

        elif self.PATIENT == None and FORM_LOCK:
            **DISABLED = True**
            INITIAL = None
       
        self.fields["pat"] = forms.ModelChoiceField(queryset = Patient.objects.all(),widget=forms.HiddenInput(), label = "Patient", initial=INITIAL)
        # insert forms fields
        self.fields['eli_dat'] = forms.DateField(label = 'Date (de remplissage)',widget=forms.TextInput(attrs={'autocomplete': 'off'}),required=False, disabled=DISABLED)
        self.fields['eli_oui_ebo'] = forms.TypedChoiceField(label = 'A eu, dans les 72h, un contact avec un malade Ebola confirmé par RT-PCR',widget=forms.RadioSelect(attrs={'readonly': True}),required=False,choices=[i for i in Thesaurus.options_list(1,'fr') if i != (None,'')], disabled=DISABLED)
      

template.html

<form id="id-form" method="post">
   {% csrf_token %}
   {% crispy form %}
</form>
Mereva
  • 350
  • 2
  • 14
  • 1
    Does this answer your question? https://stackoverflow.com/questions/1953017/why-cant-radio-buttons-be-readonly – Jimmy Pells Feb 08 '22 at 12:56
  • thanks for replying; in fact I resolved issue using widget=forms.RadioSelect(attrs={'disabled': DISABLED}) (and not 'readonly') – Mereva Feb 08 '22 at 14:14

0 Answers0