django crispy forms readonly

thumbtackthief picture thumbtackthief · Feb 4, 2014 · Viewed 8.1k times · Source

I'm trying to make some of my fields in my crispy form readonly. I would have thought it would be simple, but it's not working. No errors, just not readonly.

class ApplicationForm(BaseItemForm):

    jira_bucket = forms.CharField(
        widget=autocomplete_light.ChoiceWidget('JiraAutocomplete'))

    def __init__(self, *args, **kwargs):
        ''' Change form based on permissions of the user '''
        super(ApplicationForm, self).__init__(*args, **kwargs)
        # app = self.instance

        if not self.user.has_perm('Assets.sarbox_fields'):

            disabled_widget = forms.CheckboxInput(attrs={'readonly': True})
            self.fields['sarbox'].widget = disabled_widget
            self.fields['pii'].widget = disabled_widget
            self.fields['pci'].widget = disabled_widget
            if 'sarbox_approver' in self.fields:
                self.fields['sarbox_approver'].widget.attrs['disabled'] = True
        # it's about to get crispy
        self.helper = FormHelper()
        # self.helper.form_class = 'application-form'  # done in template
        self.helper.form_tag = False
        self.helper.disable_csrf = True
        self.helper.layout = Layout(
            Div(
                Field('name'),
                Field('jira_bucket'),
            ),
            Row(Field('primary_tech',
                      wrapper_class='col-md-6'),
                Field('primary_biz',
                      wrapper_class='col-md-6')
                ),
            Row(Field('backup_tech',
                      wrapper_class='col-md-6'),
                Field('backup_biz',
                      wrapper_class='col-md-6'),
                ),
            Field('desc', rows=2),
            Div(
                Field('sarbox'),
                Field('pci'),
                Field('pii'),
                css_class='form-inline',
                ),
            Row(
                Field('sarbox_tech_approvers',
                      wrapper_class='col-md-6'),
                Field('sarbox_biz_approvers',
                      wrapper_class='col-md-6'),
                css_id='sarbox_approver'
                ),
            )

    def clean(self):
        cleaned_data = self.cleaned_data
        sarbox = cleaned_data.get('sarbox')
        if sarbox:
            if not (cleaned_data['sarbox_tech_approvers'] and
                    cleaned_data['sarbox_biz_approvers']):
                raise forms.ValidationError("Sarbox applications must have Sarbox Tech and Business approvers assigned.")
        return cleaned_data

Answer

Matt picture Matt · Apr 15, 2015

I had to set the readonly argument to True in the Layout, like so:

class ContactForm(forms.Form)
  email = forms.EmailField(label='Email')
  api_token = forms.CharField(label='API Token')

  def __init__(self, *args, **kwargs):
    super(ContactForm, self.__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.layout = Layout(
      'email',
      Field('api_token', readonly=True),
    )