AndrewIngram / django-extra-views

Django's class-based generic views are awesome, let's have more of them.
MIT License
1.39k stars 172 forks source link

Management form issue with named inline forms #168

Closed slandau3 closed 6 years ago

slandau3 commented 6 years ago

I tried implementing a model that is referenced by several foreign keys and the html appears to generate fine but when I attempt to submit it I get "ManagementForm data is missing or has been tampered with"

My template is as follows:

  <form method="post" action="." id="update-form">
    {% csrf_token %}
    {% crispy form %}
    {% if matching %}
        {{ matching.management_form }}
        <fieldset>
          <legend>Matches</legend>
          {% for  match in matching.forms %}
            <div class="matching-formset mb-3">
              {% crispy match %}
            </div>
          {% endfor %}
        </fieldset>
    {% endif %}
    {{ additional_files.management_form |crispy}}
    <fieldset>
      <legend>Additional Files</legend>
      {% for file in additional_files.forms %}
        <div class="additional-data-formset mb-3">
          {% crispy file %}
        </div>
      {% endfor %}
    </fieldset>

    <button type="submit"  class="save" value="submit"/>
  </form>

On the page that's rendered I give users the ability to add and delete forms. I use django-dynamic-formsets to do this. Here's how I initialize it (it appears to be working and updates the management forms properly):

        $('.additional-data-formset').formset({
            prefix: '{{ additional_files.prefix }}',
            addText: 'Add File',
            deleteText: 'Delete File',

    });
        $('.matching-formset').formset({
          prefix: '{{ matching.prefix }}',
          addText: 'Add Match',
          deleteText: 'Delete Match',

        });

Another thing, for some reason the submit button has not been working. I've been having to submit the form manually via a jquery event

    $('.save').on('click', function(e){$('#update-form').submit();})

I followed the documentation to design all my views but I'll post them here just in case

class MatchingOptionInline(extra_views.InlineFormSet):
    model = MatchingOption
    fields = ('key', 'value',)
    factory_kwargs = {'extra': 1}
    prefix = "match"

class AdditionalDataInline(extra_views.InlineFormSet):
    model = AdditionalData
    form_class = forms.AdditionalDataForm
    fields = ('upload',)
    factory_kwargs = {'extra': 1}
    prefix = "file"

class MatchingView(
                       extra_views.NamedFormsetsMixin,
                       extra_views.CreateWithInlinesView):
    model = Matching
    form_class = forms.MatchingForm
    template_name = 'pages/editors/question.html'
    inlines = [MatchingOptionInline, AdditionalDataInline]
    inlines_names = ['matching', 'additional_files']

    success_url = reverse_lazy('home:home')
    prefix = "main-matching-view"

All of the form_class's that are referred to are just simple model forms.

This issue has been extremely annoying and I'm not sure what's causing it.

jonashaag commented 6 years ago

This is too specific of a case, please generalize by removing code and components until you have a minimal reproducing example. First you should get rid of the JS stuff and see if the problem is gone. Then remove crispy, etc.

jonashaag commented 6 years ago

Closing due to inactivity.