内联表单验证错误不在模板中
inline forms validation errors not in template
我有这个型号:
class dateEvent(models.Model):
link = models.URLField(null=True, blank=True)
link_description = models.CharField(max_length=30, null=True, blank=True)
event = models.ForeignKey('Event', on_delete=models.CASCADE)
class Event(models.Model):
title = models.CharField(max_length=200)
[...]
views.py:
def event_edit_view(request, id):
event = get_object_or_404(Event, id=id)
form_event = EventForm(request.POST or None), instance=event)
DateEventFormSet = inlineformset_factory(Event, dateEvent, extra=5, can_delete=True, fields=('event', 'start_date_time', 'venue', 'link', 'link_description'),
widgets={
'venue': s2forms.Select2Widget(),
'link': forms.TextInput(attrs={'placeholder': 'http://'}),
'start_date_time': CalendarWidget(),
'description_link': forms.TextInput(attrs={'placeholder': 'Link description'}),
})
form_date_event = DateEventFormSet(request.POST or None, instance=Event.objects.get(id=id), prefix="dateEvent", queryset=dateEvent.objects.filter(event__id=id))
if request.method == "POST":
if form_event.is_valid() and request.POST['action'] == 'submit':
if form_date_event.is_valid():
form_event.save()
form_date_event.save()
messages.success(request, 'Event updated successfully. See the <a href="/event-detail/' + str(id) + '">detail page</a>')
return redirect('my-events')
else:
raise forms.ValidationError(form_date_event.errors)
elif form_event.is_valid() and form_date_event.is_valid() and request.POST['action'] == 'update':
form_event.save()
form_date_event.save()
else:
raise forms.ValidationError([form_event.errors, form_date_event.errors])
context = {
'event': event,
'id': event.id,
'form_event': form_event,
'form_date_event': form_date_event,
}
return render(request, "events/event-edit.html", context)
和模板:
<tbody id='date_body'>
{{ form_date_event.management_form }}
{{form_date_event}}
</tbody>
当我在模板上输入非法数据时(例如,URL 字段中的非 URL)Django 通过丑陋的调试界面抛出错误:
ValidationError at myurl...
['Enter a valid URL.']
Request Method: POST
或者如果关闭调试,则会出现服务器错误 500。为什么我没有在模板上显示错误?
应该在表单而不是视图中引发 ValidationError。通过在视图中引发 ValidationError,您不会呈现模板。这解释了为什么您可以在调试数据中看到错误,但没有呈现页面。 500 错误很可能是因为您的视图在引发错误时未返回 HTTP 响应。该文档包含如何引发视图和表单错误的示例。
我有这个型号:
class dateEvent(models.Model):
link = models.URLField(null=True, blank=True)
link_description = models.CharField(max_length=30, null=True, blank=True)
event = models.ForeignKey('Event', on_delete=models.CASCADE)
class Event(models.Model):
title = models.CharField(max_length=200)
[...]
views.py:
def event_edit_view(request, id):
event = get_object_or_404(Event, id=id)
form_event = EventForm(request.POST or None), instance=event)
DateEventFormSet = inlineformset_factory(Event, dateEvent, extra=5, can_delete=True, fields=('event', 'start_date_time', 'venue', 'link', 'link_description'),
widgets={
'venue': s2forms.Select2Widget(),
'link': forms.TextInput(attrs={'placeholder': 'http://'}),
'start_date_time': CalendarWidget(),
'description_link': forms.TextInput(attrs={'placeholder': 'Link description'}),
})
form_date_event = DateEventFormSet(request.POST or None, instance=Event.objects.get(id=id), prefix="dateEvent", queryset=dateEvent.objects.filter(event__id=id))
if request.method == "POST":
if form_event.is_valid() and request.POST['action'] == 'submit':
if form_date_event.is_valid():
form_event.save()
form_date_event.save()
messages.success(request, 'Event updated successfully. See the <a href="/event-detail/' + str(id) + '">detail page</a>')
return redirect('my-events')
else:
raise forms.ValidationError(form_date_event.errors)
elif form_event.is_valid() and form_date_event.is_valid() and request.POST['action'] == 'update':
form_event.save()
form_date_event.save()
else:
raise forms.ValidationError([form_event.errors, form_date_event.errors])
context = {
'event': event,
'id': event.id,
'form_event': form_event,
'form_date_event': form_date_event,
}
return render(request, "events/event-edit.html", context)
和模板:
<tbody id='date_body'>
{{ form_date_event.management_form }}
{{form_date_event}}
</tbody>
当我在模板上输入非法数据时(例如,URL 字段中的非 URL)Django 通过丑陋的调试界面抛出错误:
ValidationError at myurl...
['Enter a valid URL.']
Request Method: POST
或者如果关闭调试,则会出现服务器错误 500。为什么我没有在模板上显示错误?
应该在表单而不是视图中引发 ValidationError。通过在视图中引发 ValidationError,您不会呈现模板。这解释了为什么您可以在调试数据中看到错误,但没有呈现页面。 500 错误很可能是因为您的视图在引发错误时未返回 HTTP 响应。该文档包含如何引发视图和表单错误的示例。