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 gracefully call one interface to another interface in Django

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

Share

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

It is believed that many inexperienced people don't know what to do about how to elegantly call other interfaces in one interface in Django. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

In an actual scenario encountered in development, a new API interface is added to django, and the function of this interface part needs to use the return data of another interface.

A less elegant solution is to call another interface in the form of a HTTP request in the new interface, which is theoretically feasible.

But it also brings a series of problems, such as performance concurrency and so on. After all, it takes a certain amount of time for HTTP communications to establish connections.

A better solution is to call the view function of the front interface in the new interface by calling the function. As we all know, Django's request data is wrapped in a HttpRequest object, since we are going to call the view function of another interface

Then you need to encapsulate the HttpRequest object, so it is necessary to know about the HttpRequest object

The following is the diagram I debugged, and the property values of the request object can be seen at a glance.

We don't need to pay attention to anything else here, we just need to modify the relevant data of the request.

GET class dictionary object containing all GET parameters POST containing all POST parameters headeers request header related method request method body request body, where the data of the POST method is obtained

OK, knowing the data related to the request mentioned above, we can construct our own request body, and then call the front interface.

Here is a small problem to note that body is the data type of bytes, so when assigning values, you need to convert the dictionary type to bytes. Here is the original data of the request.

Next, I will re-assign the data type to request.body after being encoded with data

> data = {"name": "jerry", "code": "Python"}

> import json

> data = json.dumps (data) .encode (encoding= "utf-8")

> data

B'"{\\" name\ ":\\" jerry\ ",\\" code\ ":\\" Python\ "}"'

> request.body = data

After typing the last line of assignment code, an error was reported.

The hint cannot be reset because the body property is not modifiable. Fortunately, django has another parameter, _ body, and the body property inherits from the _ body property, so it is fine to modify the _ body property directly.

> request._body = data

> data

B'"{\" name\ ":\\" jerry\ ",\\" code\ ":\\" Python\ "}"

After the assignment, let's look at the value of body.

As you can see, it has been modified to the data we want. Then pass the request object to the previous interface and you're done!

After reading the above, have you mastered how to elegantly call one interface to another in Django? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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