第一节了解如何使用Django写接口

1先创建一个app:

python manage.py startapp Api

2在setting里安装app:

INSTALLED_APPS = [
'Api'
]

3在视图view里编写函数:

from django.http import JsonResponse
def response_json(self):
  result = {"name":"老王"}
  return JsonResponse(result)

4定义路由:

urlpatterns = [
path('api/response_json/',response_json),
]

5启动项目

python manage.py runserver

6使用postman测试:

workspacess --Myworkspaces--Postman API--Collections--GET ALL Collections

http://127.0.0.1:8000/api/response_json/

posted @ 2021-11-20 22:59  linuxTang  阅读(534)  评论(0)    收藏  举报