1.djangoå¦ä½å°è£
apiï¼djangoå°è£
exeï¼
djangoå¦ä½å°è£ apiï¼djangoå°è£ exeï¼
æ¬ç¯æç« ç»å¤§å®¶è°è°djangoå¦ä½å°è£ apiï¼ä»¥ådjangoå°è£ exe对åºçç¥è¯ç¹ï¼å¸æ对åä½ææ帮å©ï¼ä¸è¦å¿äºæ¶èæ¬ç«åãæ¬æç®å½ä¸è§ï¼
1ãï¼äºï¼Django RESTå®è·µï¼æç®åçREST APIå®ç°2ãPython3.7é åDjango2.0æ¥è°ç¨éé(dingding)å¨çº¿apiå®æ¶çæµåå·¥èå¤æå¡æ åµ3ãDjango REST frameworkæ¡æ¶ä¹GET,远程源码远程源码用机关网站源码 POST, PUT, PATCH, DELETEçAPI请æ±æ¥å£è®¾è®¡ï¼äºï¼Django RESTå®è·µï¼æç®åçREST APIå®ç°æ¬å°è大æ¦è¦è±è´¹åéã
å¨åé¢ï¼æ们已ç»å¦ä¼äºDjangoå¦ä½è·å¾HTTP请æ±ä¸çå 容ï¼ä»¥åå¦ä½è·åHTTP请æ±çbodyãæ¥ä¸æ¥æ们就æ¥åä¸ä¸ªæç®åçAPIãè¿ä¸ªAPIè¦æ±å¨è¯·æ±çHTTP bodyä¸æ¾å ¥JSONæ ¼å¼çææ¬ï¼å¹¶å¨è§£æææ¬åè¿è¡å¤çï¼è¿åJSONæ ¼å¼çæ°æ®ã
æ们å®ä¹ APIçURL为/api/sum/ï¼åè½æ¯ä¸ºä¸¤ä¸ªæ°æ±åï¼å¹¶è¿åã
requestä¸bodyçæ ¼å¼ä¸ºï¼
responseçæ ¼å¼ä¸ºï¼
responseä¸ï¼æ们çæ°æ®ç»æç¨å¾®æç¹å¤æã
ä¹åæ们ææçREST APIé½ä¼ä»¥è¿ç§ç»ä¸çæ ¼å¼è¿åæ°æ®ï¼ä¸¤ä¸ªä¸åREST APIæè¿åå 容ç主è¦åºå«å¨dataåä¸ã
å¨ï¼ä¸ï¼ä¸ï¼æ们建ç«äºä¸ä¸ªå«åtask_platformçDjango项ç®ãç®å½ç»æå¦ä¸ï¼
è¿å ¥Django项ç®ç®å½ï¼å¹¶ç¼è¾task_platformä¸çviews.pyæ件ã
å ¶ä¸ï¼
ç¼è¾task_platform/urls.pyï¼å°æ们ååå®ç°çAPIå¤çå½æ°å å ¥å°è·¯ç±è¡¨ä¸ã
è¿è¡Django serverï¼
ç¨Postman模æ请æ±ï¼å¯ä»¥çå°ï¼
ç»è¿ç»ä¹ ï¼æ们已ç»äºè§£å¦ä½å®ç°ä¸ä¸ªç®åçREST APIäºï¼æ们ä¹åçAPIé½æ¯å»ºç«å¨è¿ä¸ªé讯模åä¹ä¸çï¼å½ç¶è¿æä¸äºä½¿ç¨GETæ¹æ³çAPIï¼ãå¨åé¢ï¼æ们å°ççï¼å¦ä½éè¿è¿ç§æ¨¡å¼ï¼å®ç°ä¸ä¸ªç¨æ·è®¤è¯ç³»ç»çREST APIã
Python3.7é åDjango2.0æ¥è°ç¨éé(dingding)å¨çº¿apiå®æ¶çæµåå·¥èå¤æå¡æ åµæ°å ç«æ æé´ï¼å¤§å¤æ°å ¬å¸ä¸ºäºé¿å 交åææé½æå¤æå°çéç¨äºè¿ç¨åå ¬çæ¹å¼ï¼è¿æ¾ç¶æ¯ä¸ä¸ªææºçéæ©ï¼åºæ¬ä¸éé(dingding)ä½ä¸ºä¸ä¸ªè¿ç¨åå ¬å¹³å°æ¥ç¨çè¯ï¼è½ç¶å·®å¼ºäººæï¼ä½æ¯å¥ä½å¸é¢ä¸æ²¡æå¥æ´å¥½çéæ©ï¼ç¬åéæå°åï¼ä¹è¿æ¯å¯ä»¥ååç¨çï¼ä¸è¿è¿ç¨åå ¬æ个é®é¢ï¼å°±æ¯æ¯å¤©éè¦æ£æ¥åå·¥çèå¤ï¼å± 家åå ¬è½ç¶çµæ´»ï¼ä½æ¯å¤§å®¶ç©¶ç«æ没æåå ¬ï¼åæ¯å¦å¤ä¸åäºï¼ééæä¾ç解å³æ¹æ¡å°±æ¯èå¤å¨çº¿æå¡åè½ï¼ä½æ¯æ£æ¥åºå¤ééå¨ç§»å¨ç«¯å°±æç¹è´¹å²ï¼éè¦å¨ééappéç¹å»è³å°5次,è¿ä¸è½å®æ¶å·æ°ï¼pc端çééoaç³»ç»åçæ´çï¼è¿ä¸å¦ç§»å¨ç«¯æ¥å¾æ¹ä¾¿ï¼å¦å¤å¦æä½ å¨ä¸å®¶ä¸å人çä¼ä¸éï¼è¿å®¶ä¼ä¸æ大大å°å°å å个é¨é¨ï¼ä½ åé常åéçæ ä»»è¿å®¶å ¬å¸ç人äºä¸»ç®¡ï¼æ¯å¤©æé¨é¨æ¥åºåå·¥èå¤æ¥è¡¨å°±ä¸æ¯ä¸ä»¶å®¹æäºäºï¼æ以å©ç¨ééå¼æ¾çæ¥å£ï¼ä½¿ç¨Djangoèªå·±æé ä¸å¥å®æ¶çæ§åå·¥èå¤çwebå¹³å°æ¯æ们æ¬æ¬¡çç®çã
项ç®èæ¯æ¯ä¸å®¶æ®éç§æå ¬å¸ï¼å¤§æ¦æäºä¸ªé¨é¨ï¼æ¯ä¸ªé¨é¨äººå·¦å³
é¦å è¿å ¥ééå¼æ¾å¹³å° :open-dev.dingtalk.com
å¨ä¼ä¸å é¨å¼åä¸ï¼éæ©å°ç¨åºï¼æ°å»ºä¸ä¸ªå°ç¨åºåºç¨ï¼è¿éå ¶å®ä¹è¿æå«çéæ©ï¼æ¯å¦h5å¾®åºç¨ï¼ä¸»è¦æ¯å°ç¨åºå ¼å®¹æ§æ´å¥½ä¸ç¹ã
å¡«ååºç¨çå称ãç®ä»ãLogoçåºæ¬ä¿¡æ¯è¿äºæä¸ä¸è¡¨ï¼æç §è¦æ±å¡«åå³å¯ï¼ä¹ä¸å¿ éå¾å¡«åçå®ä¿¡æ¯ï¼è¿éæ个åå°±æ¯ä¸å®ä¸è¦å¿äºé ç½®å®å ¨ååæè ipï¼å®å ¨ååæ¯å½æ们çæ£æµå¹³å°ä¸çº¿çæ¶åé¨ç½²çååï¼åºç¨å¯ä»¥è·æå®çååè¿è¡ç½ç»éä¿¡ï¼å¦æä¸é ç½®çè¯ï¼è¯·æ±ééæ¥å£ä¼æ¥é误ã
å¦å¤è¿æä¸ä¸ªåï¼ä¹å°±æ¯ééé»è®¤å¼æ¾çæ¥å£ä» éäºåºç¡æéæ¥å£
å¦æéè¦èå¤æè ç¾å°æ¥å£çè¯ï¼è¿å¾åç¬ç¹å»ç³è¯·ï¼è¿å°±æç¹è®©äººçä¸æäºï¼é£ä¹å¤æ¥å£ï¼å ¨é½å¾é ç¨é¼ æ ç¹å»å¼éï¼ä¸å¼éå°±ç¨ä¸äºï¼è¿ä¸ªç¨æ·ä½éªçæ¯è®©äººéå¸¸é ¸ç½ï¼äº§å设计æè¿æ ·ï¼ééçpmé¾è¾å ¶åã
OKï¼åç½®åå¤å·¥ä½å°±å·²ç»å°±ç»ªäºï¼ç°å¨æ们åªè¦æ ¹æ®å®æ¹ææ¡£æ¥åæ¥å£å°±å¯ä»¥äºï¼éæ©æå¡ç«¯apiææ¡£ï¼
ééèå¤æå¡çæ¥å£è¯´ææ¯è¿æ ·çï¼
è¿éæ¯ä¸ªæ¥å£é½éè¦ä¸ä¸ªaccess_tokenç¨æ¥é´æï¼è¿ä¸ªtokenæ¯ç¨idåç§é¥éè¿æ¥å£äº¤æ¢åæ¥çï¼å ·ä½å¨åºç¨è¯¦æ éå¯ä»¥è·å
è¿éæ们å°è£ ææ¹æ³
æå®äºtokenï¼è¿éè¦è·åæ¨çé¨é¨ä¸ææåå·¥çåå·¥idï¼å 为èå¤æ¥å£åæ°åªè½æ¥ååå·¥idï¼èéé¨é¨id
æå请æ±èå¤æ¥å£å³å¯
å®æ´çåå°Djangoåå°æ¥å£
è¿æ ·ï¼å°±å¯ä»¥æå¿«çéè¿çº¿ä¸å¹³å°æ¥å®æ¶çæµé¨é¨åå·¥èå¤äºï¼æææ¯è¿æ ·ç:
Django REST frameworkæ¡æ¶ä¹GET, POST, PUT, PATCH, DELETEçAPI请æ±æ¥å£è®¾è®¡
ä¸ãAPIæ¥å£åè½éæ±ï¼è®¾è®¡ä¸äºæ¥å£URLï¼è®©å端/客æ·è¯·æ±è¿ä¸ªURLå»è·åæ°æ®å¹¶æ¾ç¤ºï¼æ´æ¹æ°æ®ï¼å¢å æ¹æ¥ï¼ï¼è¾¾å°åå端å离çææ
äºã设计é»è¾ï¼éè¿httpå议请æ±æ¹å¼GETãPOSTãPUTãPATCHãDELETE设计符åRESTfulè§èçapiæ¥å£ä¹å°±æ¯URL
ä¸ãç®ææºç ï¼
3.åºååserializers
#å¯¼å ¥æ¨¡åç±»årest_frameworkåºåå模åserializers
from .models import Article
from rest_framework import serializers
#å®ä¹åºååç±»ï¼ä½¿ç¨ç»§æ¿ModelSerializeræ¹æ³
class ArticleSerializer(serializers.ModelSerializer):
class Meta:
model = Article#æå®åºååç模åç±»
fields = '_ all _'#éååºååå段ï¼æ¤å¤å¯èªè¡éåå段
4.è§å¾å½æ°views
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from .models import Article
from .serializers import ArticleSerializer
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
#è°ç¨csrfè£ é¥°å¨csrf_exempt模åï¼è§£å³è·¨å访é®é®é¢
#JSONRendererå®å°Pythonçdict转æ¢ä¸ºJSONè¿åç»å®¢æ·ç«¯
#JSONParser è´è´£å°è¯·æ±æ¥æ¶çJSONæ°æ®è½¬æ¢ä¸ºdict
#åæ³ä¸
#å¨éè¦è·¨åçè§å¾ä¸è°ç¨è£ 饰å¨@csrf_exempt
@csrf_exempt
def article_list(request):
if request.method == 'GET':
arts = Article.objects.all()#è·å模åç±»æ°æ®
ser = ArticleSerializer(instance=arts,many=True)#åºååæ°æ®instance
#ä¸ä¸æ¥ç¨rest_frameworkæ¹æ³éçJSONRendereræ¹æ³æ¸²ææ°æ®
json_data = JSONRenderer().render(ser.data)
return HttpResponse(json_data,content_type='application/json',status=)
#åæ³äº
class JSONResponse(HttpResponse):
def _ init(self,data,**kwargs):
content = JSONRenderer().render(data)
kwargs['content_type'] = 'application/json'
super(JSONResponse, self)._ init(content,**kwargs)
#æ ¹æ®idè¿è¡å¢å æ¹æä½æ¥å£
@csrf_exempt
def article_detail(request,id):
try:
art = Article.objects.get(id=id)
except Article.DoesNotExist as e:
return HttpResponse(status=)
å¤æ³¨ï¼
*åæ³äºä¸å®ä¹JSONResponseç±»å°è¿åçæ°æ®dataä¸content_typeè¿åç±»ååäºå°è£
*APIæ¥å£
GET/POST
GET/PUT/PATCH/DELETE
*Postmanæµè¯ææå¾
djangoå¦ä½å°è£ apiçä»ç»å°±èå°è¿éå§ï¼æè°¢ä½ è±æ¶é´é 读æ¬ç«å 容ï¼æ´å¤å ³äºdjangoå°è£ exeãdjangoå¦ä½å°è£ apiçä¿¡æ¯å«å¿äºå¨æ¬ç«è¿è¡æ¥æ¾åã