text: Q:
  
  Custom ModelForm with FormHelper re-appends buttons Div after each refresh when a layout is passed
  
  To keep my ModelForms DRY I have created a custom ModelForm with a FormHelper so I can append a Div with a Submit and a Cancel button to the layout. It also offers the possibility to add a custom layout.
  This works perfectly fine when I don't specify a custom layout, but when I do, every time I refresh the page it appends the buttons Div (this doesn't happen when there's no custom layout)
  This is the custom ModelForm:
  class ModelFormWithHelper(ModelForm):
  
      def __init__(self, *args, **kwargs):
          super().__init__(*args, **kwargs)
          kwargs = self.get_helper_kwargs()
          helper_class = FormHelper(self)
          if 'custom_layout' in kwargs:
              self.helper.layout = kwargs['custom_layout']
          self.helper.form_class = 'form-horizontal'
          self.helper.label_class = 'col-md-12'
          self.helper.field_class = 'col-md-12'
          self.helper.layout.append(
              Div(
                  HTML('<br>'),
                  FormActions(
                      Submit('submit', 'Save'),
                      HTML('<a class="btn btn-secondary" href="{{ request.META.HTTP_REFERRER }}">Cancel</a>')
                  ),
                  css_class='row justify-content-center',
              ),
          )
  
      def get_helper_kwargs(self):
          kwargs = {}
          for attr, value in self.Meta.__dict__.items():
              if attr.startswith('helper_'):
                  new_attr = attr.split('_', 1)[1]
                  kwargs[new_attr] = value
          return kwargs
  
  And this is the ModelForm:
  class CargoForm(ModelFormWithHelper):
      class Meta:
          model = Cargo
          exclude = []
          helper_custom_layout = Layout(
              Div(
                  'name',
                  'order',
                  css_class='col-6 offset-3',
              ),
          )
  
  This is the form with no custom_layout after I refresh the page 3 times:
  
  And this is the form with a custom_layout after I refresh the page 3 times:
  
  I'm aware that I can use the self.helper.add_input method to avoid this problem, but then I would not be able to center de buttons.
  I would appreciate if anyone can help me solve this re-appending problem.
  Thanks in advance.
  
  A:
  
  After almost having a mental breakdown, I have finally figured it out.
  For anyone trying to achieve the same goal, here's the solution (I got rid of the get_helper_kwargs method since I only use helper_custom_layout)
  # Put it in a variable to avoid repeating it twice
  buttons_layout = Layout(
      Div(
          HTML('<br>'),
          FormActions(
              Submit('submit', 'Save'),
              HTML('<a class="btn btn-secondary" href="{{ request.META.HTTP_REFERER }}">Cancel</a>')
          ),
          css_class='row justify-content-center',
      ),
  )
  
  class ModelFormWithHelper(ModelForm):
  
      def __init__(self, *args, **kwargs):
          super().__init__(*args, **kwargs)
          custom_layout = getattr(self.Meta, 'helper_custom_layout', None)
          self.helper = FormHelper(self)
          self.helper.form_class = 'form-horizontal'
          self.helper.label_class = 'col-md-12'
          self.helper.field_class = 'col-md-12'
          if custom_layout:
              self.helper.layout = Layout(custom_layout, buttons_layout)
          else:
              self.helper.layout.append(buttons_layout)
  
  And that's it. Now you can make your custom Layouts a little bit more DRY. Hope you can find this useful. 
  
  