django的CBV与FBV

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)