简体   繁体   中英

Overwrite Submit button label in django crispy forms

I have an upload form which works fine. The idea is that someone uploads a file and chooses a title. The file gets saved to the server and can't be changed but the title can. So I'm looking for a way to reuse the same form and change the label on the submit button from "Upload File" to "Save Changes".

This is my upload form:

class MyForm(forms.Form):
  field_1 = forms.FileField()
  field_2 = forms.CharField()
  
  def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_method = 'post'
        
        self.helper.layout = Layout(
            Row( Column('field_1', css_class='form-group col-md-12 mb-0') ),
            Row( Column('field_2', css_class='form-group col-md-12 mb-0') ),
            Submit('submit', 'Upload File', css_class='btn-success', formnovalidate='formnovalidate')) 

Now I want to create an "edit-variation" of the same form. The File field is changed to a CharField and made read only. It shows the server path of the file and can't be changed. Now I'm trying to change the button label.

class MySecondForm(MyForm):
  field_1 = forms.CharField()
  
  def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['field_1'].widget.attrs['readonly'] = True
        self.helper['submit'].label = "Save Changes"

The above does not show an error but does not work. The label remains "Upload File". I could of course just copy and change my FormHelper layout but my actual form has over 20 fields and I would rather not just copy paste the whole block just to change one label.

Is there a way to set the label of an existing Submit?

You can definitely do this, Here's an updated version of your "edit" form, with a custom init method that updates the Submit button:

class MySecondForm(MyForm):
    field_1 = forms.CharField()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['field_1'].widget.attrs['readonly'] = True
        # From crispy_forms.layout.Layout.__init__, it sets up self.fields 
        # as a list, which we can now alter b/c they've been created in the
        # parent class. Let's target the Submit() item, which is the last 
        # item in layout.fields:
        self.helper.layout.fields[-1].value = "Save Changes"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM