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

ModelFormSetView doesn't save changes #208

Closed faulander closed 4 years ago

faulander commented 4 years ago

I have two problems, i have a flat table with settings in my db, structure is: setting: char value: char

First Problem: It shows an empty field combination at the end: image

Second problem: It doesn't save the data, no error, no redirect to the success page, nothing.

my views.py:

class UpdateSettings(ModelFormSetView):
    model = Settings
    fields = ['setting', 'value']
    template_name = 'settings.html'
    success_url = '/shows'

my settings.html:

{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
    <form method="post">
        {% csrf_token %}
        {{ formset.management_form }}
        {% for form in formset %}
            <div class="row">
                <div class="col-6">            
                    {{ form.setting|as_crispy_field }}
                </div>
                <div class="col-6">            
                    {{ form.value|as_crispy_field }}
                </div>
            </div>
        {% endfor %}
        <div class="form-group form-actions">
            <button class="btn btn-success">Change</button>
        </div>
    </form>
{% endblock %}
jonashaag commented 4 years ago

Maybe the form has errors and you’re not seeing them because you’re not rendering errors in the template

sdolemelipone commented 4 years ago

And to answer your first problem, that blank row at the end is how you'd define new rows in your settings table. It's there because extra=1 is the default value for django.forms.formsets.formset_factory(). If you don't want an extra form, set

class UpdateSettings:
    ...
    factory_kwargs = {'extra': 0}

this is in the docs.