zoukankan      html  css  js  c++  java
  • django中FBC和CBV模式的urls和views的对应写法

    FBV

    urls:

    urlpatterns = [

      url(r'^admin/', admin.site.urls),
      url(r'^home/', views.home),
    ]

    views:

    def home(request):
       return render(request, 'home.html')

    CBV

    urls:

    urlpatterns = [

       url(r'^admin/', admin.site.urls),
     url(r'^home/', views.Home.as_view()), #固定写法 
    ]

    views:

    from django.views import View
    class Home(View):

      def get(self,request):
        print(request.method)
        return render(request, 'home.html')

      def post(self,request):
        print(request.method,'POST')
        return render(request, 'home.html')

    方式二:

      

    from django.views import View
    class Home(View):

      def dispatch(self, request, *args, **kwargs):   #反射调用 类中的方法     其实和上面的写法一样
        # 调用父类中的dispatch
        print('before')
        result = super(Home,self).dispatch(request, *args, **kwargs)
        print('after')
        return result

      def get(self,request):
        print(request.method)
        return render(request, 'home.html')

      def post(self,request):
        print(request.method,'POST')
        return render(request, 'home.html')

  • 相关阅读:
    DYCom多平台聊天室Sample
    silverlight多人在线游戏示例
    设计模式之: 命令模式
    Vector
    flashBuilder加入AsDoc注释
    转:每个Flash开发者应该知道的事情
    flashBuilder无法调试:57%就不动了
    UML类图实例
    Dictionary
    Enterprise Architect使用(一)
  • 原文地址:https://www.cnblogs.com/Samuel-Leung/p/10812738.html
Copyright © 2011-2022 走看看