Django

django的CBV与FBV

2017-07-06  本文已影响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/')
上一篇下一篇

猜你喜欢

热点阅读