Django的extra_views,ModelFormSetView不会保存数据。

huangapple go评论71阅读模式
英文:

Django extra_views, ModelFormSetView doesn't save the data

问题

第一个问题:在最后显示了一个空的字段组合。

第二个问题:它不保存数据,没有错误,也没有重定向到成功页面,什么都没有。

你的views.py代码如下:

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

你的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 %}

希望这对你有所帮助。如果有任何其他问题,请随时提出。

英文:

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

It shows the form just fine, but:

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

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 = [&#39;setting&#39;, &#39;value&#39;]
    template_name = &#39;settings.html&#39;
    success_url = &#39;/shows&#39;

my settings.html:

{% extends &#39;base.html&#39; %}
{% load crispy_forms_tags %}
{% block content %}
    &lt;form method=&quot;post&quot;&gt;
        {% csrf_token %}
        {{ formset.management_form }}
        {% for form in formset %}
            &lt;div class=&quot;row&quot;&gt;
                &lt;div class=&quot;col-6&quot;&gt;            
                    {{ form.setting|as_crispy_field }}
                &lt;/div&gt;
                &lt;div class=&quot;col-6&quot;&gt;            
                    {{ form.value|as_crispy_field }}
                &lt;/div&gt;
            &lt;/div&gt;
        {% endfor %}
        &lt;div class=&quot;form-group form-actions&quot;&gt;
            &lt;button class=&quot;btn btn-success&quot;&gt;Change&lt;/button&gt;
        &lt;/div&gt;
    &lt;/form&gt;
{% endblock %}

答案1

得分: 1

你的第一个问题:默认情况下,Django的modelformset_factory(在视图中使用的后台工具)会创建一个额外的空表单。为了避免这个问题,可以在你的视图中添加factory_kwargs

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

你的第二个问题:我注意到表单集(formset)出现错误,因为'id'字段是必需的。在你的模板中添加'id'字段:

<div class="row">
   <div class="col-6">
       {{ form.id }}            
       {{ form.setting|as_crispy_field }}
   </div>
   <div class="col-6">            
      {{ form.value|as_crispy_field }}
   </div>
</div>

我认为在你的视图中不需要将'id'添加到'fields'中,因为它默认会被添加。

英文:

Your first problem: By default the django modelformset_factory (which the view uses in the background) creates one extra empty form. To avoid this add the factory_kwargs to your view

class UpdateSettings(ModelFormSetView):
    model = Settings
    fields = [&#39;setting&#39;, &#39;value&#39;]
    template_name = &#39;settings.html&#39;
    success_url = &#39;/shows&#39;
    factory_kwargs = {&#39;extra&#39;: 0}

Your second problem: I saw the formset is throwing an error because the 'id' field is required. Add the 'id' to your template:

&lt;div class=&quot;row&quot;&gt;
   &lt;div class=&quot;col-6&quot;&gt;
       {{ form.id }}            
       {{ form.setting|as_crispy_field }}
   &lt;/div&gt;
   &lt;div class=&quot;col-6&quot;&gt;            
      {{ form.value|as_crispy_field }}
   &lt;/div&gt;
&lt;/div&gt;

i don't think 'id' needs to be added to the 'fields' in your view as it is added by default

huangapple
  • 本文由 发表于 2020年1月3日 19:08:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定