django - 如何将 django 表单错误作为文本而不是 html?
问题描述
我有一个显示错误的祝酒词。
$.toast({
text: '{{ form.non_field_errors }}'
})
但我只想从{{ form.non_field_errors }}
. 我怎么做?
解决方案
您可以.as_text()
在以下位置使用该方法non_field_errors
:
$.toast({
text: '{{ form.non_field_errors.as_text }}'
})
但是请注意,为了使其更安全,您最好将其 JSON 化,并将输出标记为安全。
因此,您最好在视图中进行一些处理:
from json import dumps as jdumps
def some_view(request):
some_form = ModelForm(request.POST)
errors = jdumps(some_form.non_field_errors().as_text())
return render(request, 'some_template.html', {'errors': errors})
并在模板中将其呈现为:
$.toast({
text: {{ errors|safe }}
})
推荐阅读
- php - 您的要求无法解析为一组可安装的软件包 php ini (windows)
- ios - SwiftUI:UIAlertController 的 textField 在 UIAlertAction 中没有响应
- c - 减少 GTK+ 中的按钮高度
- julia - 返回一元数组作为元素类型
- java - 我在这里缺少什么或者我应该做不同的事情?
- android - Sinch 应用程序到应用程序的语音通话不起作用
- django - Django elasticsearch dsl没有识别我创建的索引
- c# - Application.Run() 破坏其他功能
- c++ - CPP:string.find()==0 和 string.find !=string::npos 之间的区别
- logging - 使用单个 awk 命令将日期格式从文本更改为数字