Django modelformset如果实例已经存在,如何重定向
我正在使用modelformset_工厂创建多个实例。现在,当用户返回页面,现有实例填充表单时,如果用户单击submit,错误将显示实例已经存在,这是由唯一的_-together约束触发的Django modelformset如果实例已经存在,如何重定向,django,django-forms,Django,Django Forms,我正在使用modelformset_工厂创建多个实例。现在,当用户返回页面,现有实例填充表单时,如果用户单击submit,错误将显示实例已经存在,这是由唯一的_-together约束触发的 formset_errors [{'__all__': ['course with this instructor, location, program already exists.']}, {'__all__': ['course with this instructor, location, p
formset_errors
[{'__all__': ['course with this instructor, location, program already exists.']},
{'__all__': ['course with this instructor, location, program already exists.']},
{}]
我想将用户重定向到listview以显示这些实例,并设置一条消息,说明我们没有创建实例,因为它们已经在数据库中了。我该怎么做?现在,我使用的不是最好的:
redirect_already_exists = True
for form_error in formset_errors: # list of dictionaries
if form_error.get('__all__'):
error_message = form_error['__all__'][0]
if 'already exists' not in error_message:
redirect_already_exists = False
if redirect_already_exists:
redirect_page = reverse('course:list_courses', args = (program_id,))
return HttpResponseRedirect(redirect_page)