av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

python - django源碼探究,as_view()的具體分發(fā)過程?

瀏覽:117日期:2022-07-28 15:16:26

問題描述

最近在學(xué)習(xí)django的類視圖,就打開源碼學(xué)習(xí)下,但是對基類View的as_view方法不太理解,先把源碼貼上來:

@classonlymethod def as_view(cls, **initkwargs):'''Main entry point for a request-response process.'''for key in initkwargs: if key in cls.http_method_names:raise TypeError('You tried to pass in the %s method name as a ''keyword argument to %s(). Don’t do that.'% (key, cls.__name__)) if not hasattr(cls, key):raise TypeError('%s() received an invalid keyword %r. as_view ''only accepts arguments that are already ''attributes of the class.' % (cls.__name__, key))def view(request, *args, **kwargs): self = cls(**initkwargs) if hasattr(self, ’get’) and not hasattr(self, ’head’):self.head = self.get self.request = request self.args = args self.kwargs = kwargs return self.dispatch(request, *args, **kwargs)view.view_class = clsview.view_initkwargs = initkwargs# take name and docstring from classupdate_wrapper(view, cls, updated=())# and possible attributes set by decorators# like csrf_exempt from dispatchupdate_wrapper(view, cls.dispatch, assigned=())return view

因為最后涉及View的另外的一個方法dispatch,我也貼出這個方法源碼:

def dispatch(self, request, *args, **kwargs):# Try to dispatch to the right method; if a method doesn’t exist,# defer to the error handler. Also defer to the error handler if the# request method isn’t on the approved list.if request.method.lower() in self.http_method_names: handler = getattr(self, request.method.lower(), self.http_method_not_allowed)else: handler = self.http_method_not_allowedreturn handler(request, *args, **kwargs)

當(dāng)類視圖調(diào)用as_view方法時,會把請求時的request方法自動對應(yīng)到相應(yīng)的類方法上,比如request的get方法對應(yīng)到類視圖的get方法。

但是我看完源碼的理解是:as_view僅僅能自動對應(yīng)get和post(具體的request方法在類屬性當(dāng)中有個列表:http_method_names = [’get’, ’post’, ’put’, ’patch’, ’delete’, ’head’, ’options’, ’trace’])等方法,如果我在類視圖定義了自己的方法,那as_view并不能把我自定義的方法對應(yīng)起來

但是,同樣是類視圖,ListView當(dāng)中卻有g(shù)et_queryset方法,那ListView在調(diào)用as_view方法時會自動調(diào)用這個get_queryset方法嗎(它并不是request的方法是吧?)?

代碼哪里提到了這個過程呢?

望大神指教~抱拳

問題解答

回答1:

dispath方法里就是根據(jù)request的方法尋找class view對應(yīng)的函數(shù)處理:handler = getattr(self, request.method.lower(), self.http_method_not_allowed)

ListView中的get_queryset方法是別的函數(shù)調(diào)用的

標(biāo)簽: Python 編程
主站蜘蛛池模板: 国产黄色片视频 | 欧美aaaaa| 欧美日韩毛片 | 亚洲黄色在线视频 | 国产永久视频 | www.一区二区三区 | 国产精品2区 | 天美传媒在线观看 | 黄av在线| 欧美精品www| 深夜福利久久 | 久久久久国产一区二区三区 | 亚洲日本中文字幕 | 欧美日韩91 | 天堂中文av | 久久精品国产亚洲 | 国产成人免费 | 黄色免费片 | 久久精品国产成人av | 国产精品高清在线观看 | 在线日韩| 亚洲毛片av | 日韩看片 | 免费看av的网址 | 久在线视频| 第一福利丝瓜av导航 | 视频一区二区在线播放 | av片网站| 91狠狠综合 | 亚洲精品久久久久avwww潮水 | 一级黄色大片 | 国产一区二区三区久久 | 久久精品一区二区国产 | 中文字幕在线观看网站 | 日韩欧美国产精品 | 日韩午夜片 | 久久九九精品 | 国产欧美日韩视频 | 久久av一区二区三区亚洲 | 高潮毛片无遮挡免费看 | 国产成人精品一区二区三区四区 |