1.djangoå¦ä½å°view设为å
¨å±(djangoçviewå¦ä½å¤çº¿ç¨)
2.djangoå¦ä½å°è£
apiï¼djangoå°è£
exeï¼
djangoå¦ä½å°viewè®¾ä¸ºå ¨å±(djangoçviewå¦ä½å¤çº¿ç¨)
导读ï¼æ¬ç¯æç« é¦å¸CTOç¬è®°æ¥ç»å¤§å®¶ä»ç»æå ³djangoå¦ä½å°viewè®¾ä¸ºå ¨å±çç¸å ³å 容ï¼å¸æ对大家ææ帮å©ï¼ä¸èµ·æ¥ççå§ãå¦ä½DjangoçView类工ä½è¿äºviewsçåºç¡æ¶åç¸å½é«çº§çPythonï¼æ以å¦æä½ æ¯ä¸ä¸ªåå¦è ï¼ç¸å¯¹çï¼å¦æä½ åç°è¿ä¸ªä»£ç ï¼è¿å¹¶ä¸å¥æª
æ¨åºè¯¥æç½æ主è¦çæ¯ä»ä¹@classmethoddecoratorä¸åçå®ä¹as_view()ãé£æ¯ä¸æ¯è¿å°±æ¯æè°çç±»çä¸ä¸ªå®ä¾ï¼ä»¥åå¨éè¦å®ä¾ä½ä¸ºselfä½æ¯è¿å°±æ¯æè°çç±»æ¬èº«ï¼å¹¶éåäºç±»ä½ä¸ºclsè¯è¨æ¯æï¼ä½ä¸ºä¸ä¸ªè½ç¶å¨Pythonä¸ï¼è¿æ¯ä¸ä¸ªç¬¬ä¸ç±»CodeGo.netï¼æ们ä¸éè¦å¨è¿éèµè¿°äºã
è¿æ¯ä¸ä¸ªææ ·ççæ³æ¯å¨URLconfä¸å®ä¹ãä½ ææ£ç¡®WelcomeView.as_view()-è¿æ¯ä»ä¹åçæ¯è°ç¨as_viewå¨è¯¥URLé ç½®æ¯å¯¼å ¥çã
å 为æ们ç¥éï¼ä»ç¹1ï¼clsæ¯è§å¾ç±»æ¬èº«ãæ£å¸¸çä¸ç±»ï¼å½ä½ è°ç¨å®ï¼ä½ ä¼å¾å°ä¸ä¸ªå¯¹è±¡ãæ以ï¼å°±åä½ è¯´çï¼æ们å¨è¿éåçæ¯ä»ä¹å®ä¾åçç±»ï¼ç¶ååé 该å®ä¾å为åéselfï¼ä»¿ä½æ们æ¯è¯¥å®ä¾çå é¨ãè¿éçè¦ç¹æ¯ï¼æ£å¦æä¸é¢æ说ï¼as_view被称为å¨å¯¼å ¥åå®è¿åä¸ä¸ªå½æ°-view-è¿æ¯åè¿æ¥è°ç¨çURLè°åº¦ï¼å½æµè§å¨è¯·æ±çURLãå æ¤ï¼è¯¥å½æ°å é¨ï¼æ们æ建åè°ç¨ç±»çå ¶ä½é¨åï¼ææäºåºäºç±»çè§å¾ãè³äºä¸ºä»ä¹éè¦å®ï¼è¯·åé ä¸æã
该__init__æ¹æ³å©ç¨è®¾ç½®çæ¤çinitargs以ä¸ä¸ªå®ä¾å±æ§ï¼å¨è¿éä½ å¯ä»¥éè¿è®¿é®å®å¨ä½ çè§å¾ä»£ç self.whateverè¯æ³ã
é£ä¹ï¼ä¸ºä»ä¹è¿ä¸åæå¿ è¦åï¼
åºäºç±»çè§ç¹å ·æ巨大çæ½åçé¾æçï¼è¿æ¯ä»»ä½ç±»å«ç´æ¥å¨URLé ç½®ï¼æå ¶ä»å°æ¹å¨æ¨¡å级å«ï¼å®ä¾åå°ä¼æç»æ´ä¸ªè¿ç¨çå ¨é¨ãèä¸Djangoçé¨ç½²æ¹å¼-éè¿WSGI-ä¸ä¸ªè¿ç¨å¯ä»¥æç»å¾å¤å¾å¤çè¦æ±ãå¦æä½ å·²ç»å¨å¤ä¸ªè¯·æ±åæï¼ä½ æçæ£è®¨åç线ç¨å®å ¨æ¼æ´çæ¦ç-å¦æä½ è®¾ç½®ä¸ºä¸ä¸ªè¯·æ±ä¸ä¸ªå®ä¾å±æ§ï¼ä¾å¦ï¼å®ä¼æ¾ç¤ºå¨åç»ç请æ±ã
å æ¤ï¼è¿æ®µä»£ç ä¸ä» ä¿è¯äºæ¯ä¸ªè¯·æ±å¾å°ä¸ä¸ªæ°çå®ä¾ï¼è¿ä¹ä½¿å¾å®ççå¾é¾éè¿å¨ææé å®ä¾çæ¯ä¸ªè§å¾å½æ°å æç ´è¿ä¸è¦æ±é离ã
djangoè§å¾ä¸æä¹æä»å端è·åçuserä½ä¸ºå ¨å±åé
ææ¶åï¼æ们éè¦Templates模æ¿é¡µé¢å¯ä»¥ä½¿ç¨ä¸äºåéãè¿äºåéæ们å¨views.pyååºæ¶æ²¡æè¿å设置çåéãä¾å¦ï¼å¦ä¸ä»£ç ï¼
#coding:utf-8
from?ucosii源码下载django.shortcuts?import?render
def?index(request):
context?=?{ }
context['title']?=?'æµè¯æ é¢'
return?render(request,?'index.html',?context)
ä¸é¢æ¯æ个views.pyçæ¹æ³ä¹ä¸ãå®å°æ¸²æindex.html模ç(Template)页é¢ï¼å¹¶è¿åcontextåå ¸ã该åå ¸æ¯ä¼ å ¥åéä¿¡æ¯ç»å端页é¢ã对åºçindex.htmlå¦ä¸ï¼
html
head/head
body
h3{ { title}}/h3
pæ¯å¦ç»å½ï¼{ { request.user.is_authenticated}}/p
/body
/html
ååºç»æé¤äºætitleåéå¼ä¹å¤ï¼è¿ææ¯å¦ç»å½ä¿¡æ¯ã该ç»å½ä¿¡æ¯æ¥èªrequeståéï¼é®é¢æ¯ä¸é¢views.pyä¸è¿åç»æçcontextä¸æ²¡æåå ¥requeståéãè模çä¹æ²¡å´æå¯ä»¥è·å该åéã
è¿ä¸ªå½æ¶ä¸æ¯æ ä¸çæï¼æä¸æ¥ä¸æ¥åæç»å¤§å®¶çãåç讲æç½ä¹åï¼å°±èªç¶æå¾å¦ä½è®¾ç½®æ¨¡ç(Templates)çå ¨å±åéæè å«é»è®¤åéã
renderæ¹æ³æ¯render_to_responseæ¹æ³çç®åæ¹å¼ãä¸é¢çviews.py代ç ç¸å½äºå¦ä¸ï¼
#coding:utf-8
from?django.shortcuts?import?render_to_response
from?django.template?import?RequestContext
def?index(request):
context?=?{ }
context['title']?=?'æµè¯æ é¢'
return?render_to_response('index.html',?context,?RequestContext(request))
å¦æå»ærender_to_responseç第ä¸ä¸ªåæ°ï¼å³RequestContext(request)é¨åã
渲æindex.html模ç页é¢å°±æ æ³å¾å°{ { request.user.is_authenticated}}çå¼ï¼å³æ²¡æä¼ érequeståéç»å端页é¢ãå¾ææ¾RequestContextå¾å ³é®ã
æå ³RequestContextçå 容å¯ä»¥ä»Djangoå®æ¹ææ¡£æ¥å¾ã
该类å®ä¾åæ¶ä¼è§£æsettingsä¸çTemplates设置ä¸çcontext_processorsé ç½®ãæ°å»ºDjango项ç®settings.pyæ件ä¸é»è®¤çTemplates设置å¦ä¸ï¼
TEMPLATES?=?[
{
'BACKEND':?'django.template.backends.django.DjangoTemplates',
'DIRS':?[],
'APP_DIRS':?True,
'OPTIONS':?{
'context_processors':?[
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
大家å¯åç°context_processorsæä¸ç³»å设置ï¼å ¶ä¸æ ¹æ®django.template.context_processors.requestçè·¯å¾æ¾å°Djangoçç¸å ³æºç ã
Djangoå®è£ å¨Pythonçå®è£ ç®å½ä¸Lib/site-packages/ç®å½ä¸ï¼æ¾å°django/template/context_processors.pyæ件ï¼æå¼å¯çå°requestæ¹æ³ï¼
def?request(request):
return?{ 'request':?request}
该æ¹æ³è¿åä¸ä¸ªåå ¸ï¼key为requestï¼value为request对象ãå¾ææ¾ï¼renderä¸çrequest对象就æ¯éè¿å è½½settingsä¸çcontext_processorså表æ¹æ³å¾å°åå ¸é¡¹ã
æ们ä¹å¯ä»¥éç¨è¿ç§æ¹æ³ï¼ç»Django项ç®è®¾ç½®å ¨å±ç模çåéãä¾å¦ï¼æçDjangoå称为myprojectï¼å¨myproject/myprojectç®å½ä¸å建ä¸ä¸ªcontexts.pyæ件ï¼ä»£ç å¦ä¸ï¼
#coding:utf-8
from?django.conf?import?settings
#?å¾å°è¯è¨è®¾ç½®
def?lang(request):
return?{ 'lang':?settings.LANGUAGE_CODE}
该æ件çæ¹æ³éè¦requeståæ°ï¼æåéè¦è¿åä¸ä¸ªåå ¸å³å¯ã
åæå¼settings.pyæ件ï¼å¨Templatesä¸æ·»å åæåçæ¹æ³å¼ç¨ï¼
TEMPLATES?=?[
{
'BACKEND':?'django.template.backends.django.DjangoTemplates',
'DIRS':?[],
'APP_DIRS':?True,
'OPTIONS':?{
'context_processors':?[
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
#?èªå®ä¹æ¨¡çå ¨å±åé(é»è®¤åé)
'myproject.contexts.lang',
],
},
},
]
æ·»å 模çå ¨å±åéä¹åï¼æ们å¯ä»¥å¨ä»»æä½ç½®æ¸²æ模ç页é¢æ éåæå¨åç¸å ³ä»£ç å³å¯ä½¿ç¨è¯¥åéã
9ãDjangoè§å¾ï¼Viewä¸APIViewï¼djangoä¸ç¼è¾è§å¾views.pyæ两ç§æ¹å¼ï¼ä¸ç§æ¯åºäºç±»çå®ç°ï¼å¦å¤ä¸ç§æ¯å½æ°å¼çå®ç°æ¹å¼ï¼ä¸¤ç§æ¹æ³é½å¯ä»¥ç¨ã
RESTæ¡æ¶æä¾äºä¸ä¸ªAPIViewç±»ï¼å®æ¯DjangoViewç±»çåç±»ã
Viewæ¯Djangoé»è®¤çè§å¾åºç±»ï¼APIViewæ¯RESTframeworkæä¾çææè§å¾çåºç±»,继æ¿èªDjangoçViewï¼å¯¹Djangoä¸çViewè¿è¡äºæå±ï¼å ·å¤äºè®¤è¯ãææãéæµãä¸å请æ±æ°æ®ç解æçåè½ã
a.ç»ä¸ä½¿ç¨Request对象.dataå±æ§å»è·åjsonæ ¼å¼çåæ°ãform表ååæ°ãFILES
bã使ç¨Request对象.query_paramsæ¥è·åæ¥è¯¢å符串åæ°
cãDjangoæ¯æçåæ°è·åæ¹å¼ï¼DRFé½æ¯æ
a.对Djangoä¸çHttpResponseè¿è¡äºæå±
b.å®ç°äºæ ¹æ®è¯·æ±å¤´ä¸Acceptåæ°æ¥å¨æè¿å
c.é»è®¤æ åµä¸ï¼å¦æä¸ä¼ Acceptåæ°æè ä¼ å为application/jsonï¼é£ä¹ä¼è¿åjsonæ ¼å¼çæ°æ®
d.å¦æAcceptåæ°ä¸ºtext/htmlï¼é£ä¹ä¼è¿åå¯æµè§çapi页é¢ï¼html页é¢ï¼
e.Response第ä¸ä¸ªåæ°ä¸ºï¼ç»è¿åºååä¹åçæ°æ®ï¼å¾å¾éè¦ä½¿ç¨åºååå¨å¯¹è±¡.dataï¼
f.statusæå®ååºç¶æç
å 使ç¨djangoèªå¸¦çviewï¼è·åä¸ä¸ªCard表éé¢çå¡å·ä¿¡æ¯ï¼
models.py设计card表
views.pyè§å¾çç¼å
urls.py设置访é®å°å
RESTframeworkçAPIView继æ¿äºdjangoçViewç±»ï¼å åºååCardç±»ï¼è¿éçåºååç¨rest_frameworkéé¢çModelSerializer
é ç½®urls.py,设置访é®å°å
Djangoä¸viewså¦ä½è®¾ç½®å ¨å±åéé®é¢å¨äºtest=1å®é ä¸æ¯å®ä¹äºä¸ä¸ªå±é¨åétestï¼å®éèäºå ¨å±ä½ç¨åä¸çteståéã
è¦ææ使ç¨å ¨å±çteståéï¼éè¦ä½¿ç¨globalå ³é®åã
from?django.
å¨ä¼ä¸å é¨å¼åä¸ï¼éæ©å°ç¨åºï¼æ°å»ºä¸ä¸ªå°ç¨åºåºç¨ï¼è¿éå ¶å®ä¹è¿æå«çéæ©ï¼æ¯å¦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çä¿¡æ¯å«å¿äºå¨æ¬ç«è¿è¡æ¥æ¾åã