来自视图的标准化 JSON 响应
当我的页面将表单发布到我的 Django 视图时,该视图返回包含一些数据的响应,但很快我就遇到了我的视图以不同格式和不同信息集返回数据的问题。我考虑过使用 JSON 作为标准格式来返回我的数据。
有两种类型的状态:成功
和失败
。当 POST 成功时,它只返回 success
但当失败时,它会返回一个名为 errors
的子组,其中包含一个字段和该字段的错误。
这是一个失败
响应示例:
{"failure": {
"errors":[
{"fieldname": "<fieldname>",
"fielderror": "<fielderror>"
},
{"fieldname": "<fieldname>",
"fielderror": "<fielderror>"
}]
}}
这是一个成功
响应示例:(
{"success": {
"data":[
{"fieldname": "<fieldname>",
"fielddata": "<fielddata>"
},
{"fieldname": "<fieldname>",
"fielddata": "<fielddata>"
}]
}}
成功响应具有数据字段,因为您经常希望返回一些数据,即新创建的数据库记录的键。 )
这就是我想出的,但是有很多人使用 Django,我想知道是否有一个标准的方法/更强大的方法或一些模块来帮助解决这个问题。
谢谢。
When my page POSTs a form to my Django view, the view returns a response with some data but soon I ran into the issue that my views returned data in different formats and different sets of information. I've thought of using JSON as a standard format to return my data.
There are two types of statuses, success
and failure
. When the POST was successful, it just returns success
but when it has failed, it returns a sub group called errors
which contains a of fields and that field's error.
Here's a sample failure
response:
{"failure": {
"errors":[
{"fieldname": "<fieldname>",
"fielderror": "<fielderror>"
},
{"fieldname": "<fieldname>",
"fielderror": "<fielderror>"
}]
}}
Here's a sample success
response:
{"success": {
"data":[
{"fieldname": "<fieldname>",
"fielddata": "<fielddata>"
},
{"fieldname": "<fieldname>",
"fielddata": "<fielddata>"
}]
}}
(The success response has data fields because quite often you like to return some data i.e. key of newly created DB record.)
This is what I've come up with but so many people using Django out there, I'm wondering whether there is a standard way/more robust of doing this or some module to help with this.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
去过那里。我自己编写了解决方案(因为它很简单。我不知道是否有一个模块可以解决这个问题)。这只是一个 json 响应包装器
之类的东西。就我而言,我使
Success
和Failure
接受字符串和字典作为参数(例如Success(data)
),以使其更加容易如果您的结构非常复杂(或者您太懒而无法手动编写它们),请为该复杂结构编写您自己的包装器(这样您就可以用更少的打字创建响应),并使成功/失败接受它作为参数。
been there. I wrote the solution my own (since it's simple. I dont know if theres a module out there for this). This is just a json response wrapper
something like that. In my case, I make
Success
andFailure
accept a string and a dictionary for arguments (likeSuccess(data)
) to make it even easierIf your structure is quite complex (or youre too lazy too write them manually), write your own wrapper for that complicated structure (so you can create the response with even less typing), and make the Success/Failure accepts that as argument.
你知道如何使用Python字典吗?如果是这样,那么有一些库可以将 Python 字典转换为有效的 JSON。例如,Django 的内置是
simplejson
。这里有一个例子:Do you know how to use Python dicts? If so then there are libraries which convert your Python dict into valid JSON. Django's built-in is for example is
simplejson
. Here an example:好吧,我不知道这对你有多大帮助,但 Python 确实有一个模块
json
。Well, I don't know how much this will help you out, but Python does have a module
json
.我认为前端将会为你应该如何格式化你的数据提供一个更有力的案例,然后是一些“标准”,不同的客户端框架期望不同的东西,并更好或更差地适应与期望的差异。
我试图让自己的生活更轻松的一种方法是让响应的“形状”始终大致相同,例如,响应将始终具有“状态”属性,该属性始终具有“错误”属性,并且如果没有错误,则
result.status.errors
的值为null
。如果出现错误,它将是一个字符串列表,解释出了什么问题。即使出现错误,也会有一个“value”属性,如果无法处理请求,则该属性将为 null;如果可以,则为请求的资源。I think the front end is going to make a stronger case for how you should format your data then some "standard", Different client side frameworks expect different things, and adapt to differences from that expectations better or worse than that.
One way I try to make my own life easier is to have the 'shape' of the response always roughly the same, for example, the response will always have a 'status' property, which always has an 'errors' property, and if there were no errors, then the value of
result.status.errors
isnull
. If there were errors, it'd be a list of strings explaining what went wrong. even if there were errors, there'd be a 'value' property, which would be either null if the request couldn't be serviced, or the requested resource if it could.