In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to view and debug the Django-Restful interface directly in the browser, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
I. Preface
Data parsing and rendering of Django Restful interface using built-in components of RestFramework framework
The web interfaces we have created are debugged and viewed through Postman, which calls various HTTP request methods, so that the returned results can be easily received and recognized by programming languages and applications, but it is not easy for us to view them intuitively.
Previously, when we used the OPTION method to request testing of the interface, we found that the header description of the interface prompted to render not only json data but also html data:
This is set by the django-rest-framework framework for the requester of api: by default, the interface returns the format specified by the header. For example, if we use Postman software, the default header will be application/json, which will return data in json format:
If we use the browser to access the interface, the default request header of the browser will be text/html, and the interface will return the data in the form of HTML. Let's try it out.
Second, browse the interface in the browser
Without any additional configuration, you can directly open the URL address of the API interface in the browser, and you can see the interface response data presented in HTML document rather than JSON format, as shown in the following figure:
As you can see, the response of the api interface is displayed in the browser in a friendly and beautiful HTML, and the django-rest-framework framework uses the bootstrap front-end framework to render the style of the page by default.
In addition to the simple browsing interface, the HTML-based API also provides page buttons and forms for us to debug the simple interface directly on the browser. As long as the HTTP request method allowed by the API setting is set, there will be relevant buttons on the page to provide operations, as shown in the movie list API shown below. The page provides the operation of three request methods: OPTION, GET and POST:
In the movie details API, operations of DELETE, OPTION, GET and PUT request methods are provided:
Through the operation of these pages, we no longer need to open the Postman software, but directly debug the interface in the browser.
Of course, you can also do this if you want to see the response in json format in the browser.
By default, the interface returns the HTML format in the browser, but we can use the format parameter to specify the format of the response. For example, the url that returns the list of movies in json format is:
Http://127.0.0.1:8000/movielist/?format=json
Use the format parameter to specify that the returned data is json data, then the json data will be directly rendered in the browser, as shown in the following figure:
If you change the value of the format parameter to api, it will be displayed as HTML again. Is this feature of the django-rest-framework framework useful?
After reading the above, do you know how to view and debug the Django-Restful interface directly in the browser? 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.