Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the convenient and quick json response in Django

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces you how to carry out convenient and fast json response analysis in Django, the content is very detailed, interested friends can refer to, hope to be helpful to you.

At present, in the popular front-end and front-end separate development mode, the front-end engineers need to initiate different requests to get data according to the API documents given by the back-end developers. API documents specify some request templates and response data formats and codes.

Typically, the front and back end agrees on some code to represent a meaningful response.

Here, share some techniques that an author often uses in the development process.

1 create a new utils folder

2 create a new json_status.py file

# Import module from django.http import JsonResponse

Django provides a jsonResponse method that returns a standard json response.

3 create a class of Code

Class Code: ok = 2 params_error = 1 un_auth_error = 403 server_error = 500

Return response codes for different states

4 create a standard json response function

Def result (code = Code.ok,message ='', data = None,**kwargs): json_dict = {"code": code, 'msg':message, "data": data} if kwargs and isinstance (kwargs,dict): json_dict.update (kwargs) return JsonResponse (json_dict)

Here, by defining the result function, encapsulating some commonly used response fields, such as response code, information, and data, can be used by the front end.

Kwargs: friends who are familiar with Python must be familiar with it. This is a classic use in Python. You can pass parameters of variable length to the function.

5 create richer json response functions

Ef params_error (message='',data=None):''Parameter error: param message: information passed to the frontend: param data: data passed to the frontend Dictionary type: return: Json response''return result (code=Code.params_error,message=message,data=data) def un_auth_error (code=Code.un_auth_error,message='',data=None):' 'permission error: param code:: param message:: param data:: return:' 'return result (code,message=message,data=data) def server_error (code= Code.server_error,message='') Data=None):''Server error: param code:: param message:: param data:: return:' 'return result (code,message=message,data=data)

We have created a series of json response functions above, and let's demonstrate how to use them in view functions.

6 respond in the view using the response function

# first import from utils import json_status# to create a view function def index (request): id = int (request.GET.get ("id")) if id = = 1: return json_status.result (message=' successful') else: return json_status.params_error (message=' parameter error')

Through the GET method, the value of id is obtained and the id is judged.

Return a successful response if id=1

If id is another value, the response of the parameter error is returned.

In the front end (take ajax as an example)

$.ajax ({type: get, url: url, data: {"id": 1}, success: function (result) {if (result.code = = 2) {alert (result.message);}) } else {alert (result.code); alert (result.message);} return;}})

With the above method, a standard json response can be returned. The front end operates according to different response codes.

On how to carry out convenient and fast json response analysis in Django is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report