gpt4 book ai didi

django - 在 Django 中使用 Rest api 而不是 Views

转载 作者:行者123 更新时间:2023-12-04 20:44:47 25 4
gpt4 key购买 nike

在 Django 中使用 REST api 而不是 View 的主要优势是什么

混合 REST api 和 View (一个应用程序中的 REST api 和另一个应用程序中的 View )可以吗?

还是我们需要为所有应用程序编写 REST api?而不是混合 REST 和 View

最佳答案

我假设您是在问像tastypie 或django-rest-framework 这样的图书馆的优势。

我从未使用过tastypie,但对于REST api,django-rest-framework 提供了很多开箱即用的功能,这使得这些api 的开发速度更快。

django-rest-framework提供以下内容:

  • 模型序列化程序(很像模型表单,一种将模型实例呈现给用户的快捷方式)
  • 内置 API 身份验证方案
  • 基于 Web 的免费 API 文档(只需导航到浏览器中的资源!就这么简单)

  • 来自 django-rest-framework 站点:

    Some reasons you might want to use REST framework:

    • The Web browseable API is a huge usability win for your developers.
    • Authentication policies including OAuth1a and OAuth2 out of the box.
    • Serialization that supports both ORM and non-ORM data sources.
    • Customizable all the way down - just use regular function-based views if you don't need the more powerful features.
    • Extensive documentation, and great community support.


    最终,主要优点是 django-rest-api 包含许多工具,可以更轻松、更快速地创建 rest api。 django-rest-api 所做的一切你都可以自己实现。

    两者混用绝对没问题。很多时候,一个 django 项目会有一个提供 html 内容的应用程序和另一个通过 API 公开数据的应用程序

    关于django - 在 Django 中使用 Rest api 而不是 Views,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19771438/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com