{"rsdb":{"rid":"241554","subhead":"","postdate":"0","aid":"166329","fid":"77","uid":"1","topic":"1","content":"
\n

\u9996\u5148\u5f97\u6709\u4e00\u70b9\u5e38\u8bc6\uff0c\u6bd4\u5982\u7528\u6237\u8ba4\u8bc1\uff0c\u5c31\u662fauthenticate<\/p> \n

\u6bd4\u5982\u4e00\u4e2a\u51fd\u6570\uff0c\u5e94\u8be5\u6709\u8fd4\u56de\u503c\uff0c<\/p> \n

\u6bd4\u5982\u4e00\u4e2a\u7c7b\u91cc\u9762\u7684self\uff0c\u771f\u7684\u662f\u4ee3\u8868\u672c\u8eab\u8fd9\u4e2a\u7c7b\u5417<\/p> \n

\u518d\u6bd4\u5982\u770b\u5230\u4e00\u4e2a\u4e1c\u897f\u52a0\u62ec\u53f7\uff0c\u5c31\u4e24\u79cd\u60c5\u51b5\uff0c\u4e00\u79cd\u662f\u51fd\u6570\uff0c\u4e00\u79cd\u662f\u7c7b\uff0c\u533a\u5206\u51fd\u6570\u548c\u7c7b\uff0c\u5c31\u770b\u52a0\u62ec\u53f7\u4e4b\u540e\u7684\u4e1c\u897f\uff0c\u662f\u5426\u8fd8\u8c03\u7528\u5c5e\u6027\u6216\u8005\u65b9\u6cd5<\/p> \n

\u8fd8\u6709\u5982\u679c\u4f60\u7528pycharm\uff0c\u770b\u6e90\u7801\u5c31\u5f88\u65b9\u4fbf\u4e86\uff0c\u770b\u5230\u4e00\u4e2a\u5c5e\u6027\u6216\u65b9\u6cd5\uff0c\u4f60\u9700\u8981\u505a\u7684\u5c31\u662f\u6309\u4f4fctrl\u70b9\u8fdb\u53bb\u5c31\u884c\u4e86<\/p> \n

 <\/p> \n

\u5047\u8bbe\u4f60\u5df2\u7ecf\u77e5\u9053\uff0cdispatch\u51fd\u6570\u662f\u6e90\u7801\u7684\u5165\u53e3\uff0c\u7136\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u6109\u5feb\u7684\u4ea4\u6d41\u4e86<\/p> \n

\u4e0b\u9762\u6f14\u793a\u5982\u4f55\u4f7f\u7528rest_framework\u7684\u7528\u6237\u8ba4\u8bc1\u529f\u80fd\uff0c\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u505a<\/p> \n

1.\u5199\u8def\u7531\u5339\u914d<\/p> \n

\u3000\u3000\u56e0\u4e3a\u662fcbv\u89c6\u56fe\uff0c\u6240\u4ee5\u7528as_view()\u3000\u3000<\/p> \n

\n
from<\/span> django.conf.urls import<\/span> url\n<\/span>from<\/span> django.contrib import<\/span> admin\n<\/span>from<\/span> app01 import<\/span><\/strong> views<\/strong>\n\nurlpatterns <\/span>= [\n    url(r<\/span>'<\/span>^admin\/<\/span>'<\/span>, admin.site.urls),\n    url(r<\/strong><\/span>'<\/span>^test\/<\/span>'<\/span><\/strong>, views.TestView.as_view()),<\/strong>\n]<\/span><\/pre> \n <\/div> \n 

2.CBV\u98ce\u683c\u89c6\u56fe<\/p> \n

\n
from<\/span> django.shortcuts import<\/span> HttpResponse\n\n<\/span>from<\/span> rest_framework.views import<\/span> APIView#<\/span>\u5bfc\u5165rest_framework\u7684\u8bd5\u56fe\u7c7b<\/span>\n\nclass<\/span> TestView(APIView):
\u3000\u3000self.dispatch\u3000\u3000\u3000\u3000\u3000\u3000#\u53ea\u662f\u4e3a\u4e86\u8fdb\u6e90\u7801\n <\/span>def<\/span> get(self,request,*args,**kwargs):\n <\/span>return<\/span> HttpResponse('<\/span>ok<\/span>'<\/span>)<\/pre> \n <\/div> \n

\u597d\u4e86\uff0c\u6700\u57fa\u672c\u7684\u6846\u67b6\u5c31\u8fd9\u6837\u642d\u597d\u4e86\uff0c\u6211\u4eec\u5f00\u59cb\u770b\u6e90\u7801\uff0c\u6e90\u7801\u7684\u5165\u53e3\u662f\u5728   APIView \u7684dispatch\u65b9\u6cd5\uff0c\u6240\u4ee5\u6211\u4eec\u70b9\u8fdb\u53bb<\/p> \n

 <\/p> \n

dispatch\u51fd\u6570\uff1a<\/p> \n

\n
    def<\/span> dispatch(self, request, *args, **kwargs):#<\/span>\u8fd9\u662fAPIView\u7c7b\u7684\u5165\u53e3<\/span>\n        """<\/span>\n        `.dispatch()` is pretty much the same as Django's regular dispatch,\n        but with extra hooks for startup, finalize, and exception handling.\n        \u548cDjango\u7684dispatch\u7c7b\u4f3c\uff0c\u4f46\u662f\u589e\u52a0\u4e86\u989d\u5916\u7684\u94a9\u5b50\uff0c\u6bd4\u5982\u5f00\u59cb\uff0c\u7ed3\u675f\uff0c\u4ee5\u53ca\u5f02\u5e38\u7684\u5904\u7406\n        <\/span>"""<\/span>\n        self.args <\/span>= args    #<\/span>\u5c01\u88c5\u53c2\u6570<\/span>\n        self.kwargs = kwargs\n        request <\/strong><\/span>= self.initialize_request(request, *args, **kwargs)<\/strong>     #<\/span>\u91cd\u65b0\u5c01\u88c5\u4e86request<\/span>\n        self.request = request\n        self.headers <\/span>= self.default_response_headers  #<\/span> deprecate?<\/span>\n\n        try<\/span>:\n            self.initial(request, <\/strong><\/span>*args, **kwargs) <\/span><\/strong>#<\/span> Get the appropriate handler method<\/span>\n            if<\/span> request.method.lower() in<\/span> self.http_method_names:\n                handler <\/span>= getattr(self, request.method.lower(),\n                                  self.http_method_not_allowed)\n            <\/span>else<\/span>:\n                handler <\/span>= self.http_method_not_allowed\n\n            response <\/span>= handler(request, *args, **kwargs)\n\n        <\/span>except<\/span> Exception as exc:\n            response <\/span>= self.handle_exception(exc)\n\n        self.response <\/span>= self.finalize_response(request, response, *args, **kwargs)\n        <\/span>return<\/span> self.response<\/pre> \n <\/div> \n 

\u7ecf\u8fc7\u89c2\u5bdf\uff0c\u8fd9\u4e2a\u51fd\u6570\u91cc\u9762\uff0c\u80fd\u8ba9\u6211\u4eec\u611f\u89c9\u6709\u4e1c\u897f\u7684\u5730\u65b9\u5c31\u662f\u91cd\u65b0\u5c01\u88c5request\u548cself.initial\u521d\u8bc6\u5316\u8fd9\u4e24\u4e2a\u5730\u65b9<\/strong><\/p> \n

1.1\u5148\u770b\u91cd\u65b0\u5c01\u88c5request\u8fd9\u4e2a<\/strong><\/p> \n

\n
    def<\/span> initialize_request(self, request, *args, **kwargs):\n        <\/span>"""<\/span>\n        Returns the initial request object.\n        \u8fd4\u56de\u521d\u59cb\u5316\u540e\u7684request\n        <\/span>"""<\/span>\n        parser_context <\/span>= self.get_parser_context(request)\n\n        return<\/span> Request(\n            request,\n            parsers<\/span>=self.get_parsers(),\n            authenticators<\/strong><\/span><\/span>=self.get_authenticators(), <\/strong> <\/span>     #\u91cd\u70b9\u5173\u6ce8\u8fd9\u4e2a\uff0c\u770b\u540d\u5b57\u5c31\u5e94\u8be5\u627e\u5230\u8fd9\u4e2a\uff0c\u7ee7\u7eed\u70b9\u8fdb\u53bb<\/span>\n            negotiator=self.get_content_negotiator(),\n            parser_context<\/span>=parser_context          \n        )  <\/pre> \n <\/div> \n 

1.2<\/p> \n

\n
def<\/span> get_authenticators(self):\n        <\/span>"""<\/span>\n        Instantiates and returns the list of authenticators that this view can use.\n        <\/span>"""<\/span>\n        return<\/span> [auth() for<\/span> auth in<\/span> self.authentication_classes<\/strong>] #<\/span>\u4ece\u914d\u7f6e\u8bfb\u53d6\u7528\u6237\u8ba4\u8bc1\u914d\u7f6e\uff0c\u7136\u540e\u5b9e\u4f8b\u5316\uff0c\u70b9\u8fdb\u8fd9\u4e2a<\/span>self.authentication_classes<\/strong><\/pre> \n  
<\/pre> \n <\/div> \n 

\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a\u5217\u8868\uff0c\u901a\u8fc7\u5217\u8868\u751f\u6210\u5f0f\uff0c\u5faa\u73af    self.authentication_classes   \uff0c\u7136\u540e\u62ff\u5230\u7684\u4e1c\u897f\u5b9e\u4f8b\u5316<\/p> \n

tips\uff1a\u770b\u5230\u4e00\u4e2a\u4e1c\u897f\u52a0\u62ec\u53f7\uff0c\u5c31\u4e24\u79cd\u60c5\u51b5\uff0c\u4e00\u79cd\u662f\u51fd\u6570\uff0c\u4e00\u79cd\u662f\u7c7b\uff0c\u533a\u5206\u51fd\u6570\u548c\u7c7b\uff0c\u5c31\u770b\u52a0\u62ec\u53f7\u4e4b\u540e\u7684\u4e1c\u897f\uff0c\u662f\u5426\u8fd8\u8c03\u7528\u5c5e\u6027\u6216\u8005\u65b9\u6cd5<\/strong><\/p> \n

1.3<\/p> \n

\n
class<\/span> APIView(View):\n\n    <\/span>#<\/span> The following policies may be set at either globally, or per-view.<\/span>\n    renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES        \n    parser_classes = api_settings.DEFAULT_PARSER_CLASSES\n    authentication_classes <\/strong><\/span>=<\/strong> api_settings.DEFAULT_AUTHENTICATION_CLASSES\u3000\u3000\u3000\u3000\u3000\u3000#<\/strong>\n    throttle_classes <\/span>= api_settings.DEFAULT_THROTTLE_CLASSES<\/pre> \n <\/div> \n 

\u574f\u4e86\uff0c\u8fd9\u4e0b\u4e0d\u80fd\u70b9\u4e86\uff0c\u770bsettings\uff0c\u8bf4\u660e\u8fd9\u662f\u914d\u7f6e\u4fe1\u606f\uff0c\u597d\u4e86\uff0c\u770brequest\u770b\u5230\u8fd9\u5c31\u8d70\u4e0d\u4e0b\u53bb\u4e86\uff0c\u56e0\u4e3a\u4e0d\u80fd\u70b9\u4e86\uff0c\u73b0\u5728\u6211\u4eec\u62ff\u5230\u7684\u4fe1\u606f\u662f\uff1a<\/p> \n

\u3000\u3000\u5728\u5c01\u88c5request\u7684\u65f6\u5019\uff0c \u628a\u4e00\u4e2aauth()\u5217\u8868\u4f20\u5230\u4e86request\u7684\u53c2\u6570\u91cc\u9762\uff0c\u7136\u540e\u8fd9\u4e2aauth\uff08\uff09\u662f\u4ece\u8fd9\u4e2aself.authentication_classes \u91cc\u9762\u62ff\u5230\u7684<\/strong><\/p> \n","orderid":"0","title":"\u6e90\u7801\u600e\u4e48\u627e\u4e4brest_framework\u7684\u7528\u6237\u8ba4\u8bc1(\u4e00)","smalltitle":"","mid":"0","fname":"Python","special_id":"0","bak_id":"0","info":"0","hits":"499","pages":"3","comments":"0","posttime":"2017-11-23 08:31:43","list":"1511397103","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"http:\/\/images2017.cnblogs.com\/blog\/1164899\/201711\/1164899-20171122171322586-1849192712.png","ispic":"1","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\u6e90\u7801<\/A> \u600e\u4e48<\/A> rest_framework<\/A> \u7528\u6237<\/A> \u8ba4\u8bc1<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"14.20.31.236","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u6e90\u7801\u600e\u4e48\u627e\u4e4brest_framework\u7684\u7528\u6237\u8ba4\u8bc1","lastview":"1702481178","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}