如何在 django 中序列化字典以在 Jquery 中呈现 [问题级别 - 初学者]
要序列化的字典 - 表单错误
例如查看 -
form = PersonalForm(request.POST)
# errors = serializing function which serializes form.errors
data = errors
#Is this the way to pass data? Not quite sure....
return HttpResponse(data,mimetype="application/json")
例如 javascript(请求成功) -
function(responseData) {
$('#errors_form').html(responseData);
},
现在我该怎么做,我的朋友?
The dictionary to serialize -
form.errors
e.g. view -
form = PersonalForm(request.POST)
# errors = serializing function which serializes form.errors
data = errors
#Is this the way to pass data? Not quite sure....
return HttpResponse(data,mimetype="application/json")
e.g. javascript (on success of request) -
function(responseData) {
$('#errors_form').html(responseData);
},
Now how do I do this my friends?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您询问如何将字典转换为 JSON 对象,以便您的 jQuery/javascript 可以读取它。 json.dumps 允许这种情况发生。
You're asking how to turn a dictionary into a JSON object, so your jQuery/javascript can read it. json.dumps allows this to happen.
您需要在两个地方查找错误。
有“非字段错误”:
以及基于字段的错误,例如名称字段:
根据表单的复杂性,您可以将错误作为 json 中的单独错误引用,或者制作一个将它们组合在一起的小型 python 脚本。我实际上没有运行代码,但认为这对你有用:
You will need to look in two places for errors.
There are "Non field errors":
And field based errors, for example the name field:
Depending on the complexity of the form, you could reference the errors as individual errors in your json, or make a small python script that combined them. I didn't actually run the code, but think this could work for you:
这里不是挑剔,但你真的验证了你的表格吗?
Not being picky here but did you actually validate your form ?