api 用起来很方便就像我就经常用百度的api做语音识别、人脸识别、聊天机器人等今天的笔记不是api的调用而是自己如何创建一个api
可以把机器学习的模型放在云服务器训练然后在本地调用
00模块安装
pip install django pip install rest_framework
02新建django(这部分到python搭建django网站看)这里简单讲一下(命令行输入)
django-admin startproject project_name
03新建名字为 api 的 app(命令行输入)
python manage.py startapp app_name
04setting设置
注册app
INSTALLED_APPS
setting.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', # 要使用rest_framework需要在这里添加上 ]
添加REST_FRAMEWORK
setting.py REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( # 'rest_framework.authentication.SessionAuthentication', # 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', ), 'DEFAULT_PERMISSION_CLASSES': ( # 'rest_framework.permissions.IsAuthenticated', ), }
05主url设置路由转发
urls.py from django.contrib import admin from django.urls import path, include, urlpatterns = [ path('admin/', admin.site.urls),#管理后台 path('api/', include('api.urls')),#api ] from django.conf import settings from django.conf.urls.static import static urlpatterns += static(settings.STATIC_URL,document_root=settings.STATICFILES_DIRS)
路由设置
api/url.py from django.contrib import admin from api import views #+ from django.urls import path #导入blogAPP下的views urlpatterns = [ path('admin/', admin.site.urls),#管理后台 path('login/',views.LoginView.as_view()) ] from django.conf import settings from django.conf.urls.static import static urlpatterns += static(settings.STATIC_URL,document_root=settings.STATICFILES_DIRS)
06views设置
from django.shortcuts import render from rest_framework.response import Response from rest_framework.views import APIView # Create your views here. #功能修改这里 class LoginView(APIView): def post(self,request,*args,**kwargs): print(request.data)# return Response({"status":request.data})
#aip配置就是这几步具体功能在06views里面设置