来自视图的标准化JSON响应 [英] Standardised JSON response from views

查看:283
本文介绍了来自视图的标准化JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的页面将表单发布到我的Django视图时,该视图会返回一些响应,其中包含一些数据,但是很快遇到问题,我的视图以不同的格式和不同的信息集返回数据。我以为使用JSON作为标准格式来返回我的数据。

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.

有两种类型的状态,成功失败。当POST成功时,它只返回 success ,但是当它失败时,它返回一个称为错误的子组包含一个字段和该字段的错误。

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.

以下是一个示例失败响应:

{"failure": {
    "errors":[
        {"fieldname": "<fieldname>", 
         "fielderror": "<fielderror>"
        },
        {"fieldname": "<fieldname>", 
         "fielderror": "<fielderror>"
        }]
}}

这里有一个示例成功回应:

{"success": {
    "data":[
        {"fieldname": "<fieldname>", 
         "fielddata": "<fielddata>"
        },
        {"fieldname": "<fieldname>", 
         "fielddata": "<fielddata>"
        }]
}}

(成功响应有数据字段,通常你喜欢返回一些数据,即新创建的DB记录的关键字。)

(The success response has data fields because quite often you like to return some data i.e. key of newly created DB record.)

这是我想出来的,但是有很多人在那里使用Django,我想知道是否有一个标准的方法/更强大的做这个或一些模块来帮助这个。

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.

谢谢。

推荐答案

我写了我自己的解决方案(因为它很简单,我不知道这是一个模块在那里)。这只是一个json响应包装器

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

from django.utils import simplejson
from django.http import HttpResponse

class JsonResponse(HttpResponse):
    def __init__(self, data):
        content = simplejson.dumps(data)
        super(JsonResponse, self).__init__(content=content,
                                           mimetype='application/json')

class Success(JsonResponse):
    def __init__(self, something):
        x = something # set your data here
        content = {'success': {'data': x}}
        super(Success, self).__init__(content)


class Failure(JsonResponse):
    def __init__(self, something):
        x = something # set your data
        content = {'failures': {'errors': x}}
        super(Failure, self).__init__(content)

在我的情况下,我使成功失败接受字符串和字典的参数(如成功(数据))以使其更容易

something like that. In my case, I make Success and Failure accept a string and a dictionary for arguments (like Success(data)) to make it even easier

如果您的结构相当复杂(或者您也懒得也手动写入)为这个复杂的结构编写自己的包装器(所以你可以用更少的打字创建响应),并使成功/失败接受为参数。

If 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.

这篇关于来自视图的标准化JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆