英文:
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 = ['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 %}
答案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 = ['setting', 'value']
template_name = 'settings.html'
success_url = '/shows'
factory_kwargs = {'extra': 0}
Your second problem: I saw the formset is throwing an error because the 'id' field is required. Add the 'id' to your template:
<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>
i don't think 'id' needs to be added to the 'fields' in your view as it is added by default
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论