1. 程式人生 > >Django知識梳理

Django知識梳理

命名 htm rect clu name use return redirect brush

請求周期:

    url > 路由 > 函數或類 > 返回字符串或模板語言

Form 表單提交: 

    先處理模板語言再講HTML發出去
      提交 > url > 函數或類中的方法 ————
          - httpResponse()     |
           render(request)     |
           redirect(‘/index‘)     |
           用戶 < 返回字符串 <——

       

      註:當為redirect時,自動發起另外的請求

ajax    

$.ajax({
				url:‘/index‘,
				data: {‘k‘:1,‘f‘:1},
				type:‘POST‘,
				dataType:‘JSON‘,
				traditional:true,
				success:function(d){
					location.reload		#刷新
					location.href = ‘某個地址‘  #跳轉,代替redirect
				
				}
			})

  

路由系統URL

    1.

      a. /index/
      b. /index/(\d+)
      c. /index/(?P<nid>\d+)


      d. /index/(?P<nid>\d+) name = ‘root‘
        reverse()
        {% url ‘root‘ 1%}
      e. /crm/ include(‘app01.urls‘)
      f. 默認值
        /index/ {‘web‘:‘root‘}

        def func(request,web)
          return ...
      g.命名空間
        /admin/ include(‘app01.urls‘,namespace=‘author‘)

        /crm/ include(‘app01.urls‘,namespace=‘publisher‘)

        app01.urls
        /index/ name = detail
        reverse(‘author‘:detail)

    2.

          def func(request):
            request.POST
            request.GET
            request.FILES
            request.method
            request.path_info

            return render,HttpResponse,redirect

    3.Views


          request.body
            request.POST
            request.GET
            request.FILES
            requset.xxxx.getlist

          request.Meta
            request.method
            request.path_info
            request.COOKIES

          -請求的其他信息
            from django.core.handles.wsgi import WSGIrequest

            a = ‘中國‘
            return HttpResponse(a)
            return render
            return redirect

            response[‘name‘] = ‘arnol‘ #返回到響應頭
            response.set_cookie()
            return response

裝飾器    

      FBV
      CBV
      from django.utils.decorators import method_decorator
      @method_decorator(裝飾函數)


      @method_decorator(裝飾函數)
      def dispatch(self,request):
      return super(類名,self).dispatch(request,*args,**kwargs)

      @method_decorator(裝飾函數,name=‘dispatch‘)
      class Order(Views.view)

Django知識梳理