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

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

Django extra_views, ModelFormSetView doesn't save the data

问题

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

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

你的views.py代码如下:

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

你的settings.html代码如下:

  1. {% extends 'base.html' %}
  2. {% load crispy_forms_tags %}
  3. {% block content %}
  4. <form method="post">
  5. {% csrf_token %}
  6. {{ formset.management_form }}
  7. {% for form in formset %}
  8. <div class="row">
  9. <div class="col-6">
  10. {{ form.setting|as_crispy_field }}
  11. </div>
  12. <div class="col-6">
  13. {{ form.value|as_crispy_field }}
  14. </div>
  15. </div>
  16. {% endfor %}
  17. <div class="form-group form-actions">
  18. <button class="btn btn-success">Change</button>
  19. </div>
  20. </form>
  21. {% 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:

  1. class UpdateSettings(ModelFormSetView):
  2. model = Settings
  3. fields = [&#39;setting&#39;, &#39;value&#39;]
  4. template_name = &#39;settings.html&#39;
  5. success_url = &#39;/shows&#39;

my settings.html:

  1. {% extends &#39;base.html&#39; %}
  2. {% load crispy_forms_tags %}
  3. {% block content %}
  4. &lt;form method=&quot;post&quot;&gt;
  5. {% csrf_token %}
  6. {{ formset.management_form }}
  7. {% for form in formset %}
  8. &lt;div class=&quot;row&quot;&gt;
  9. &lt;div class=&quot;col-6&quot;&gt;
  10. {{ form.setting|as_crispy_field }}
  11. &lt;/div&gt;
  12. &lt;div class=&quot;col-6&quot;&gt;
  13. {{ form.value|as_crispy_field }}
  14. &lt;/div&gt;
  15. &lt;/div&gt;
  16. {% endfor %}
  17. &lt;div class=&quot;form-group form-actions&quot;&gt;
  18. &lt;button class=&quot;btn btn-success&quot;&gt;Change&lt;/button&gt;
  19. &lt;/div&gt;
  20. &lt;/form&gt;
  21. {% endblock %}

答案1

得分: 1

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

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

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

  1. <div class="row">
  2. <div class="col-6">
  3. {{ form.id }}
  4. {{ form.setting|as_crispy_field }}
  5. </div>
  6. <div class="col-6">
  7. {{ form.value|as_crispy_field }}
  8. </div>
  9. </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

  1. class UpdateSettings(ModelFormSetView):
  2. model = Settings
  3. fields = [&#39;setting&#39;, &#39;value&#39;]
  4. template_name = &#39;settings.html&#39;
  5. success_url = &#39;/shows&#39;
  6. 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:

  1. &lt;div class=&quot;row&quot;&gt;
  2. &lt;div class=&quot;col-6&quot;&gt;
  3. {{ form.id }}
  4. {{ form.setting|as_crispy_field }}
  5. &lt;/div&gt;
  6. &lt;div class=&quot;col-6&quot;&gt;
  7. {{ form.value|as_crispy_field }}
  8. &lt;/div&gt;
  9. &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:

确定