django crispy形式只读

时间:2014-02-04 17:30:54

标签: django django-crispy-forms

我正在努力以一种脆弱的形式制作我的一些田地。我原以为这会很简单,但它不起作用。没有错误,只是不是只读。

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

5 个答案:

答案 0 :(得分:6)

制作已停用/只读的小部件:disabled_widget = forms.CheckboxInput(attrs={'disabled': True})

答案 1 :(得分:6)

我必须在布局中将readonly参数设置为True,如下所示:

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),
    )

答案 2 :(得分:2)

应该这样:

attrs={'readonly': True}

不是这个?

attrs={'readonly': 'readonly'}

答案 3 :(得分:0)

对于Columns,我必须添加一个只读类

Row(
    Column('created', css_class='col-sm-4 read-only'),
),

然后使用JS

选择输入元素
$('.read-only .form-control').prop("disabled", true);

并切换提交以使字段正确保存

$('.read-only .form-control').prop("disabled", false);

答案 4 :(得分:0)

我在动态指定只读方面遇到了一些麻烦,因为CSS似乎并不关心你设置的只读值(它只是存在使它成为只读)。我用过:

old new    
1   a  
2   dog  
3   will  
1   a   
4   be   
2   dog