一个Django应用程序来进行基于web的投票。
pyrpc-django的Python项目详细描述
Pyrpc是一个Django应用程序,用于处理JSON远程过程调用 使用Django Rest框架。在
安装
pip install pyrpc-django
快速设置
将rest_frameworkpyrpc添加到已安装的应用程序设置中,如下所示:
INSTALLED_APPS = [ ... 'rest_framework', 'pyrpc', ]
在将safe_methoddecorator添加到应用程序中的方法中。在
在
- 将pyrpc url添加到urls.py
from django.urls import path from django.conf.urls import include from pyrpc.urls import urls as pyrpc_urls urlpatterns = [ path('api/', include(pyrpc_urls)), ]
- 启动服务器:python manage.py runserver
发送请求
- 使用上面的示例,将下面的JSON发布到127.0.0.1:8000/api/methods/。在
{ "id": 1, "jsonrpc": "2.0", "method": "sum_two_numbers", "params": { "args": [], "kwargs": { "operand1": 5, "operand2": 6 } } }
- 应返回一个JSON响应,如下所示:
{ "id": 1, "jsonrpc": "2.0", "result": 11 }
返回方法列表
- 使用前面的示例,向127.0.0.1:8000/api/methods发送GET请求。在
- 方法列表及其说明应返回如下:
[ { "name": "multiply_two_numbers", "kwargs": { "operand1": "Can be any float number.", "operand2": "Can be any float number." }, "description": [ "Returns the product of two numbers.", "Extended description of function." ], "returns": "operand1 * operand2." }, { "name": "sum_two_numbers", "kwargs": { "operand1": "Can be any float number.", "operand2": "Can be any float number." }, "description": [ "Returns the sum of two numbers.", "Extended description of function." ], "returns": "operand1 + operand2." } ]
- 项目
标签: