django:表单到 json

编程入门 行业动态 更新时间:2024-10-27 04:30:00
本文介绍了django:表单到 json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的表单序列化为 json 格式.我的观点:

I am trying to serialize my form into the json format. My view:

form = CSVUploadForm(request.POST, request.FILES)
data_to_json={}
data_to_json = simplejson.dumps(form.__dict__)
return HttpResponse(data_to_json, mimetype='application/json')

我有错误 不是 JSON 可序列化的.如何处理 django 表单?

I have the error <class 'django.forms.util.ErrorList'> is not JSON serializable. What to do to handle django forms?

推荐答案

您可能需要查看名为 django-remote-forms:

You may want to look at the package called django-remote-forms:

一个允许你序列化django表单的包,包括字段和小部件到 Python 字典中,以便轻松转换为 JSON 和通过 API 公开

A package that allows you to serialize django forms, including fields and widgets into Python dictionary for easy conversion into JSON and expose over API

另见:

如何将 Django 表单转换为 dict,其中键是模板中的字段 id,值是初始值?

这篇关于django:表单到 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-28 06:06:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/720916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   django   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!