Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django crispy forms readonly

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
like image 548
thumbtackthief Avatar asked Feb 04 '14 17:02

thumbtackthief


3 Answers

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),
    )
like image 98
Matt Avatar answered Nov 03 '22 04:11

Matt


Make the disabled/readonly widget: disabled_widget = forms.CheckboxInput(attrs={'disabled': True})

like image 6
thumbtackthief Avatar answered Nov 03 '22 06:11

thumbtackthief


should this:

attrs={'readonly': True}

not be this?

attrs={'readonly': 'readonly'}
like image 2
scot Avatar answered Nov 03 '22 05:11

scot