gpt4 book ai didi

python - 自定义验证时引发 ValidationError

转载 作者:行者123 更新时间:2023-12-01 03:56:51 25 4
gpt4 key购买 nike

我无法在表单中显示错误。

假设我的表单有 2 个字段,只有当它们都为空时才不需要,但我们不能出现只有一个字段有值的情况。所以我尝试在 View 中进行此验证,然后引发异常,一切都很好,但是错误作为带有回溯等的调试页面引发。

raise utils.ValidationError(u'SOME EXAMPLE TEXT')

这是在我们的视野中。

所以我尝试了其他方法,我读到我可以在表单中以 def clean 进行验证

表格

def clean(self):
super(forms.Form,self).clean()
if 'departure_date_time_plain' in self.cleaned_data and 'arrival_date_time_plain' not in self.cleaned_data:
self._errors['arrival_date_time_plain'] = [u'PUT HERE SOME DATE']

if 'departure_date_time_plain' not in self.cleaned_data and 'arrival_date_time_plain' in self.cleaned_data:
self._errors['arrival_date_time_plain'] = [u'PUT HERE SOME DATE']
return self.cleaned_data

查看我的第二种方法的查看代码:

def new_bus(request, id, id_sec):
args = {}
args.update(csrf(request))
if request.method == 'POST':
train_form = TrainForm(request.POST)
args['new_train'] = train_form
if "train" in request.POST and train_form.is_valid():
if train_form['reservation_train'].value():
train = Train.objects.create(
transportation=train_form['trans'].value(),
number=train_form['num'].value(),
departure=train_form['departure_date_time_train'].value(),
arrival=train_form['arrival_date_time_train'].value(),
reservation=train_form['reservation_train'].value(),
seat_number=train_form['seat_number_train'].value(),
section_numbers=train_form['section_numbers_train'].value()
)
else:
train = Train.objects.create(reservation=False)

sec = Section.objects.get(id=id_sec)
sec.locomotion = JourneyType.objects.create(train=train)
sec.save()
return HttpResponseRedirect('/trip/'+ id)
args['new_train'] = TrainForm()
return render(request, 'add_trip/new_bus.html', args)

并且这个方法从未被调用过,你能告诉我如何解决这个问题吗?因为我用完了教程和任何其他选项,或者也许我只是错过了一些东西。如果有任何答复,我将不胜感激

-- 答案--

感谢@Shang Wang 的评论,我发现了真正的问题所在。仅当您的字段为 requierd=True 时,Clean 才会调用,并且由于一个错误,我的错误未正确显示:

而不是:

bus_form = BusForm(request.POST)
plain_form = PlainForm(request.POST)
train_form = TrainForm(request.POST)
args['new_bus'] = bus_form
args['new_plain'] = plain_form
args['new_train'] = train_form

我有:

bus_form = BusForm()
plain_form = PlainForm()
train_form = TrainForm()
args['new_bus'] = bus_form
args['new_plain'] = plain_form
args['new_train'] = train_form

最佳答案

我认为第二种方法的问题是您没有捕获从父 clean 方法返回的 cleaned_data 。此外,您还调用了错误的 super 方法。 super 应该在表单类本身上调用,而不是在当前类继承的类上调用。我认为你应该:

class CurrentForm(forms.Form):

def clean(self):
cleaned_data = super(CurrentForm, self).clean()
if 'departure_date_time_plain' in cleaned_data and 'arrival_date_time_plain' not in cleaned_data:
self._errors['arrival_date_time_plain'] = [u'PUT HERE SOME DATE']

if 'departure_date_time_plain' not in cleaned_data and 'arrival_date_time_plain' in cleaned_data:
self._errors['arrival_date_time_plain'] = [u'PUT HERE SOME DATE']
return self.cleaned_data

You will always have the best tutorial by checking out django docs .

关于python - 自定义验证时引发 ValidationError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37308394/

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