gpt4 book ai didi

python - 在自定义 Django Crispy 表单中忽略了 ValidationError

转载 作者:行者123 更新时间:2023-12-04 09:30:47 27 4
gpt4 key购买 nike

我有一个相当复杂的 Django 表单,它影响 3 个模型,其中一部分包括一个内联表单集。我在 https://dev.to/zxenia/django-inline-formsets-with-class-based-views-and-crispy-forms-14o6 找到了一个很好的构建表单的解决方案。 .我扩展了该解决方案并以与添加表单集类似的方式添加了第三个模型(使用自定义 Django Crispy Form 并使用 Crispy Forms Layout 功能插入它)。
我的问题是,在两个插入的表单(表单集和小子表单)中的任何一个上引发的任何验证错误都被简单地忽略了 - 主表单正确发布并且引发的 ValidationErrors 在表单中显示为错误,允许用户纠正任何错误和其数据正确保存到数据库中。如果子表单和表单集有效,它们的数据也会正确保存。但是,如果子表单和表单集中的数据无效,则表单永远不会显示错误以让用户有机会纠正他们的错误,并且数据会被简单地忽略并且永远不会保存到数据库中 - 主模型的数据保存得很好尽管。
我的问题是,如何通过添加的子表单和表单集中显示的错误刷新表单,允许用户更正错误?
下面的大部分代码来自上面引用的相当不错的帖子,并添加了第三个模型
楷模:

from django.db import models
from django.contrib.auth.models import User


class Collection(models.Model):
subject = models.CharField(max_length=300, blank=True)
owner = models.CharField(max_length=300, blank=True)
note = models.TextField(blank=True)
created_by = models.ForeignKey(User,
related_name="collections", blank=True, null=True,
on_delete=models.SET_NULL)

def __str__(self):
return str(self.id)


class CollectionTitle(models.Model):
"""
A Class for Collection titles.

"""
collection = models.ForeignKey(Collection,
related_name="has_titles", on_delete=models.CASCADE)
name = models.CharField(max_length=500, verbose_name="Title")
language = models.CharField(max_length=3)

Class CollectionTxn(models.Model):
"""
A Class for Collection transactions.

"""
collection = models.ForeignKey(Collection,
related_name="has_txn", on_delete=models.CASCADE)
number_received= models.IntegerField()
date_received= models.DateField()

class Meta:
'''
If 2 rows are entered with the same information, a validation error is raised, but it just
doesn't save the data at all instead of refreshing the form showing the error.
'''
unique_together = ('number_received', 'date_received')
表格.py:
from django import forms
from .models import Collection, CollectionTitle
from django.forms.models import inlineformset_factory
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, Fieldset, Div, Row, HTML, ButtonHolder, Submit
from .custom_layout_object import Formset, Subform

import re


class CollectionTitleForm(forms.ModelForm):

class Meta:
model = CollectionTitle
exclude = ()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

formtag_prefix = re.sub('-[0-9]+$', '', kwargs.get('prefix', ''))

self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
Row(
Field('name'),
Field('language'),
Field('DELETE'),
css_class='formset_row-{}'.format(formtag_prefix)
)
)


CollectionTitleFormSet = inlineformset_factory(
Collection, CollectionTitle, form=CollectionTitleForm,
fields=['name', 'language'], extra=1, can_delete=True
)


class CollectionForm(forms.ModelForm):

class Meta:
model = Collection
exclude = ['created_by', ]

def __init__(self, *args, **kwargs):
super(CollectionForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = True
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-md-3 create-label'
self.helper.field_class = 'col-md-9'
self.helper.layout = Layout(
Div(
Field('subject'),
Field('owner'),
Fieldset('Add titles',
Formset('titles')),
Field('note'),
Subform('transactions'),
HTML("<br>"),
ButtonHolder(Submit('submit', 'Save')),
)
)

class CollectionTxnForm(forms.ModelForm):

class Meta:
model = CollectionTxn
exclude = ()

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['collection'].widget = HiddenInput()
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
Row(
Field('number_received'),
Field('date_received'),
)
)
View .py:
from .models import *
from .forms import *
from django.views.generic.edit import CreateView, UpdateView
from django.urls import reverse_lazy
from django.db import transaction

class CollectionCreate(CreateView):
model = Collection
template_name = 'mycollections/collection_create.html'
form_class = CollectionForm
success_url = None

def get_context_data(self, **kwargs):
data = super(CollectionCreate, self).get_context_data(**kwargs)
if self.request.POST:
data['titles'] = CollectionTitleFormSet(self.request.POST)
data['transactions'] = CollectionTrxForm(self.request.POST)
else:
data['titles'] = CollectionTitleFormSet()
data['transactions'] = CollectionTrxForm()
return data

def form_valid(self, form):
context = self.get_context_data()
titles = context['titles']
transactions = context['transactions']
with transaction.atomic():
form.instance.created_by = self.request.user
self.object = form.save()
if titles.is_valid():
titles.instance = self.object
titles.save()
if transactions.is_valid():
transactions.save()
return super(CollectionCreate, self).form_valid(form)

def get_success_url(self):
return reverse_lazy('mycollections:collection_detail', kwargs={'pk': self.object.pk})
custom_layout_object.py
from crispy_forms.layout import LayoutObject, TEMPLATE_PACK
from django.shortcuts import render
from django.template.loader import render_to_string

class Formset(LayoutObject):
template = "mycollections/formset.html"

def __init__(self, formset_name_in_context, template=None):
self.formset_name_in_context = formset_name_in_context
self.fields = []
if template:
self.template = template

def render(self, form, form_style, context, template_pack=TEMPLATE_PACK):
formset = context[self.formset_name_in_context]
return render_to_string(self.template, {'formset': formset})

class SubForm(LayoutObject):
template = "mycollections/subform.html"

def __init__(self, subform_name_in_context, template=None):
self.subform_name_in_context = subform_name_in_context
self.fields = []
if template:
self.template = template

def render(self, subform, form_style, context, template_pack=TEMPLATE_PACK):
subform = context[self.subform_name_in_context]
return render_to_string(self.template, {'subform': subform})
表单集.html
{% load crispy_forms_tags %}
{% load staticfiles %}

<style type="text/css">
.delete-row {
align-self: center;
}
</style>

{{ formset.management_form|crispy }}

{% for form in formset.forms %}
{% for hidden in form.hidden_fields %}
{{ hidden|as_crispy_field }}
{% endfor %}
{% crispy form %}
{% endfor %}

<br>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{% static 'mycollections/libraries/django-dynamic-formset/jquery.formset.js' %}"></script>
<script type="text/javascript">
$('.formset_row-{{ formset.prefix }}').formset({
addText: 'add another',
deleteText: 'remove',
prefix: '{{ formset.prefix }}',
});
</script>
子表单.html
{% load crispy_forms_tags %}
{% crispy subform %}
collection_create.html
{% extends "mycollections/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
<div class="card">
<div class="card-header">
Create collection
</div>
<div class="card-body">
{% crispy form %}
</div>
</div>
</div>
{% endblock content %}
基本上,对于与添加到布局的表单集和子表单相关联的字段,仍然会引发验证错误,但它们不会冒泡到表单级别以显示错误,它们只是被忽略并且永远不会保存数据。 “主”模型工作正常,其字段显示验证错误。如果没有无效数据,则主表单、子表单和表单集的数据都正确保存。如果表单集或子表单中存在无效数据,用户将永远没有机会更正该数据。
关于我将在哪里添加所需代码的任何帮助,以便如果在表单集或子表单中输入的任何无效数据会导致表单刷新显示错误,而不是仅仅忽略和不保存无效数据,我们将不胜感激。

最佳答案

经过大量的调试和分析代码,我自己的和 Crispy 的,我已经解决了我自己的问题。这只是检查子表单和表单集验证的问题,如果无效,则重新呈现表单。
这是执行此操作的 View 中的新 form_valid() 方法:

    def form_valid(self, form):
context = self.get_context_data()
titles = context['titles']
transactions = context['transactions']
with transaction.atomic():
form.instance.created_by = self.request.user
if titles.is_valid() and transactions_is_valid():
self.object = form.save() #only save form if other subforms validate
titles.instance = self.object
# Any other field processing goes here
titles.save()
transactions.save()
else:
# If any subform or subformset is invalid, re-render the form showing errors
context.update({'titles': titles})
context.update({'transactions': transactions})
return self.render_to_response(context)
return super(CollectionCreate, self).form_valid(form)
有了这个,表单集和子表单中的任何干净的方法或者如果有任何其他错误(例如,如果表单集中没有两行相同,因为在模型中声明了 unique_together()),表单将刷新,显示所有错误三个组合的表单/表单集允许用户更正这些错误。
现在 - 用 Ajax 来做,这样页面就不会刷新:)

关于python - 在自定义 Django Crispy 表单中忽略了 ValidationError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62861445/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com