我试图只获取表单的错误消息。但是消息中会显示特殊字符。
这是我的代码:
表格.py
class FilterForm(forms.ModelForm):
...........
def clean(self):
cleaned_data = super(FilterForm, self).clean()
payee = cleaned_data.get("payee")
memo = cleaned_data.get("memo")
if not payee or not memo:
raise forms.ValidationError(
"You must put a value either for Payee or Memo")
return cleaned_data
视图.py
..............
if request.method == 'POST':
form = FilterForm(request.user, request.POST, instance=filter)
if form.is_valid():
form.save()
messages.success(request, "New rule created successfully")
else:
messages.error(request, [v[0].__str__() for k, v in form.errors.items()])
...........
当我提交无效表格时。我得到这样的输出:
['You must put a value either for Payee or Memo']
没有这个如何只得到消息[' ']
?
//the error message is shown from here
messages.error(request, [v[0].__str__() for k, v in form.errors.items()])