美文网首页Django
django的CBV与FBV

django的CBV与FBV

作者: 赖三石 | 来源:发表于2017-07-06 15:54 被阅读0次

    urls.py ------> views.py


    FBV 略


    CBV

    urls.py
    from django.conf.urls import url
    from django.contrib import admin
    from app01 import views
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^home/', views.Home.as_view(), name="home"),
    ]
    
    views.py
    from django.shortcuts import render, redirect
    from django.views import View
    # Create your views here.
    
    
    class Home(View):
    
        #判断去找get还是post的入口,再返回
        def dispatch(self, request, *args, **kwargs):
            res = super(Home, self).dispatch(request, *args, **kwargs)
            return res
    
        #普通访问走get
        def get(self, request):
            print 'get'
            return render(request, 'index.html')
    
        #submit(post)访问走post
        def post(self, request):
            print 'post'
            return redirect('/home/')
    

    相关文章

      网友评论

        本文标题:django的CBV与FBV

        本文链接:https://www.haomeiwen.com/subject/ndwhhxtx.html