I am adding custom validation to my forms and custom fields in my Django app. I would like to be able to modify the value of a field when triggering an error. For example, if there is an error, the form should be redisplayed with the field value corrected by clean() and an error message "Data has been corrected below. Click save again to confirm if these changes are OK"
我在我的Django应用程序中为我的表单和自定义字段添加自定义验证。我希望能够在触发错误时修改字段的值。例如,如果出现错误,则应使用clean()更正的字段值重新显示表单,并显示错误消息“下面已更正数据。再次单击”保存以确认这些更改是否正常“
I've tried returning the modified data in cleaned_data[] like this but it doesn't work. It displays the error correctly, but the field value is not updated with the corrected HTML when the form is redisplayed.
我已经尝试在cleaning_data []中返回修改后的数据,但是它不起作用。它正确显示错误,但在重新显示表单时,不会使用更正后的HTML更新字段值。
class T34AtividadeForm(ModelForm):
def clean(self):
# Return cleaned html
error,html = fix_imgs(cleaned_data.get("a34_descricao"))
if error:
msg = u'Data has been corrected below. Click save again to confirm if these changes are OK';
self._errors['a34_descricao'] = ErrorList([msg])
# This doesn't work
cleaned_data["a34_descricao"] = html
# This doesn't work either
self.a34_descricao = html
return cleaned_data
I'd also like to do the same thing with a field, but since the errors are triggered by exception, I don't get a chance to return the corrected value. Like the form clean() method, the error is displayed correctly, but the value is not updated.
我也想对一个字段做同样的事情,但由于错误是由异常触发的,我没有机会返回修正后的值。与表单clean()方法一样,错误也会正确显示,但值不会更新。
class HTMLField(CharField):
widget = HTMLTextarea
def clean(self, value):
value = super(HTMLField,self).clean(value)
error,html = fix_imgs(value)
if error:
# This doesn't work
self.value = html
raise forms.ValidationError(u'Data has been corrected below. Click save again to confirm if these changes are OK.')
return html
4 个解决方案
#1
6
change self data in the clean method to change the value which gets displayed
更改clean方法中的self数据以更改显示的值
#2
3
Way to update value in clean() is update the value in form's data dictionary:
在clean()中更新值的方法是更新表单数据字典中的值:
self.data["a34_stuff"] = "html"
This works for sure.
这肯定有用。
#3
0
This is the way I've tried and works for me:
这是我尝试过并为我工作的方式:
inst = my_form.save(commit=False)
if not inst.a34_stuff: # or incorrect
inst.data["a34_stuff"] = "corrected"
#4
-2
If your form is a model form a better approach would be to get an instance and correct that data instead:
如果您的表单是模型表单,则更好的方法是获取实例并更正该数据:
inst = my_form.save(commit=False)
if inst.a34_stuff is None: #or incorrect
inst.a34_stuff = "corrected"
request.user.message_set.create(message = "Error corrected")
return HttpResponseRedirect(reverse('your_url_name',
args=[])
PS: I am not sure if this will actually work... Not sure if form will pickup changes.
PS:我不确定这是否真的有效......不确定表格是否会改变。
#1
6
change self data in the clean method to change the value which gets displayed
更改clean方法中的self数据以更改显示的值
#2
3
Way to update value in clean() is update the value in form's data dictionary:
在clean()中更新值的方法是更新表单数据字典中的值:
self.data["a34_stuff"] = "html"
This works for sure.
这肯定有用。
#3
0
This is the way I've tried and works for me:
这是我尝试过并为我工作的方式:
inst = my_form.save(commit=False)
if not inst.a34_stuff: # or incorrect
inst.data["a34_stuff"] = "corrected"
#4
-2
If your form is a model form a better approach would be to get an instance and correct that data instead:
如果您的表单是模型表单,则更好的方法是获取实例并更正该数据:
inst = my_form.save(commit=False)
if inst.a34_stuff is None: #or incorrect
inst.a34_stuff = "corrected"
request.user.message_set.create(message = "Error corrected")
return HttpResponseRedirect(reverse('your_url_name',
args=[])
PS: I am not sure if this will actually work... Not sure if form will pickup changes.
PS:我不确定这是否真的有效......不确定表格是否会改变。