1.djangoå¦ä½å°view设为å
¨å±(djangoçviewå¦ä½å¤çº¿ç¨)
2.Androidä¸Viewçå建è¿ç¨
3.Android UIç»å¶ä¹Viewç»å¶çå·¥ä½åç
4.跑马灯带你深入浅出TextView的项目b项源码协作工具源码世界
5.Android 14 HWUI 源码研究 View Canvas RenderThread ViewRootImpl skia
6.ParaView 源码编译教程
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?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.puteScroll()ï¼æ们å使ç¨scrollToæ¥æView移å¨å°å½åScrolleræå¨çç»å¯¹ä½ç½®ã
/
*** Set the scrolled position of your view. This will cause a call to
* { @link #onScrollChanged(int, int, int, int)} and the view will be
* invalidated.
* @param x the x position to scroll to
* @param y the y position to scroll to
*/
public void scrollTo(int x, int y) {
if (mScrollX != x || mScrollY != y) {
int oldX = mScrollX;
int oldY = mScrollY;
mScrollX = x;
mScrollY = y;
invalidateParentCaches();
onScrollChanged(mScrollX, mScrollY, oldX, oldY);
if (!awakenScrollBars()) {
invalidate(true);
}
}
}
/
*** Move the scrolled position of your view. This will cause a call to
* { @link #onScrollChanged(int, int, int, int)} and the view will be
* invalidated.
* @param x the amount of pixels to scroll by horizontally
* @param y the amount of pixels to scroll by vertically
*/
public void scrollBy(int x, int y) {
scrollTo(mScrollX + x, mScrollY + y);
}