【问题标题】:Django Rest Framework With Only Function Response只有函数响应的 Django Rest 框架
【发布时间】:2020-12-04 09:39:53
【问题描述】:

我只想用 DRF 制作一个简单的 API。 API 将从 URL 中获取一个“关键字”,并且该“关键字”将转到一个函数。函数响应将是 API 响应。

简单示例:

def returnSomething(word):
    test_string = "This is a example sentence. "
    add_string = word
    test_string += add_string
    return test_string

http://127.0.0.1:8000/api/langdetect/helloworld/

结果:

{
  response: This is a example sentence. helloworld
}

一切都很简单。我怎样才能做到这一点?我阅读了文档,但每个示例都是使用模型、序列化程序等制作的。我没有任何模型。我只有一个函数和响应数据。

谢谢。

【问题讨论】:

标签: python django django-rest-framework


【解决方案1】:

如果您想使用 DRF 的权限和内容类型协商系统,但仍要保持简单,您将需要一个简单的 APIView

from rest_framework.views import APIView
from rest_framework.response import Response

class SomethingView(APIView):
    def get(self, request, format=None):
        word = request.data.get('word')
        return Response({'response': f'Example: {word}'})

【讨论】:

    猜你喜欢
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 2020-03-01
    • 2018-12-08
    • 2021-11-05
    • 2018-04-20
    • 2017-09-23
    相关资源
    最近更新 更多