使用REST API框架相对于Django中简单URL和视图创建的优势? [英] Advantages of using REST API framework over simple URL and view creation in Django?

查看:75
本文介绍了使用REST API框架相对于Django中简单URL和视图创建的优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于许多人来说这可能是一个愚蠢的问题,但是我为什么不能代替

It might be a silly question for many, but why can't I instead


  • 在Django中创建一个接受请求的视图并以JSON格式返回HttpResponse

  • 将视图映射到URL

  • 从我的浏览器或其他服务器上点击URL并使用结果

谢谢。

编辑-两种方法:导入一些djangorestframework或tastypie,并在我的应用程序中构建一个api,它将抛出json响应 VS 构建视图并告诉其返回json响应

EDIT - Two approaches: Import some djangorestframework or tastypie and build an api in my application which will throw json responses VS building a view and tell it to return json response. Is there any huge advantage of using the first one?

推荐答案

我认为您可以对任何扩展库提出相同的论点。这仅取决于您要重建的数量以及现有库对您的项目有什么好处。

I think you could make the same argument about any extension library. It just depends on how much you want to rebuild and what the existing library has that would be beneficial to your project.

很多时候,我创建了没有API的自定义终结点我的项目中使用ajax请求时使用库。对于该实例,使用API​​包是过大的。但是对于拥有完整的API服务器的Django rest框架,它提供了很多功能。

Many times I've created custom endpoints without an API library when working with ajax requests inside my project. For that instance, using an API package is overkill. But for having a full API server, Django rest framework offers a lot of functionality.

当然,您可以按照自己的建议进行查看。但是在某个时候,您将要通过HTTP请求进行身份验证吗?您要过滤吗?您是要授予权限,还是只是打开所有端点?您是否要防范CORS?

Sure, you can make views that do what you suggested. But at some point are you going to want to authenticate through an HTTP request? Are you going to want to filter? Are you going to want to make permissions, or just have all endpoints open? Are you going to want to protect against CORS?

您可以在API库的所有功能列表中查找有关您要完成的项目的问题。如果您正在使用任何类型的外部应用程序,并且django项目仅用于API服务器,那么<​​strong>通常通常最好使用Rest Framework。如果您只有一些一次性端点可以接收Ajax请求,则通常通常只想构建自定义端点。

You can kind of go down the list of all the features of an API library and ask these questions about what you want to accomplish with your project. If you're working with any type of external application and your django project is just for an API server, usually it's best to go with Rest Framework. If you just have some one-off endpoints to receive ajax requests, usually you just want to build custom endpoints.

这篇关于使用REST API框架相对于Django中简单URL和视图创建的优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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