Django view view
CBV and FBV
- FBV, function based view: view logic function based on
- CBV, class based view: view logic based on the class
CBV written in url
# url(r'^login/', views.login),
url(r'^login/', views.LoginView.as_view()),
View wording:
from django.views import View
class LoginView(View):
def get(self,request):
return render(request, 'login.html')
def post(self,request):
uname = request.POST.get('username')
pwd = request.POST.get('password')
if uname == 'alex' and pwd == 'dsb':
return redirect('/home/')
else:
return redirect('/login/')
Source focus. When you call to as_view method calls the dispatch route distribution method. By way of reflection, the request calls the same method.
def dispatch(self, request, *args, **kwargs): #根据请求方法去分发对应的类发放来执行
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed) #反射!!!!
else:
handler = self.http_method_not_allowed
return handler(request, *args, **kwargs)
Rewrite dispatch method
class LoginView(View):
def dispatch(self, request, *args, **kwargs):
print(11111)
# print(request.META) #http所有相关请求头信息
ret = super().dispatch(request, *args, **kwargs) #render(request, 'login.html')
print(2222)
return ret
def get(self,request):
print('this is get method!!!')
return render(request, 'login.html')
def post(self,request):
uname = request.POST.get('username')
pwd = request.POST.get('password')
if uname == 'alex' and pwd == 'dsb':
return redirect('/home/')
else:
return redirect('/login/')
CBV and FBV decorators
def func(f):
def foo(request):
print(1111)
ret = f(request)
print(2222)
return ret
return foo
# FBV模式下,和普通函数加装饰器是一样的写法
@func
def home(request):
print('home')
return HttpResponse('你好,老了老弟,进来玩会!')
# CBV加装饰的三个姿势:
from django.utils.decorators import method_decorator
# @method_decorator(func,name='get') # 位置3
class LoginView(View):
# @method_decorator(func) # 位置2
def dispatch(self, request, *args, **kwargs):
print('aaaa')
ret = super().dispatch(request, *args, **kwargs) #render(request, 'login.html')
print('bbbb')
return ret
@method_decorator(func) # 位置1
def get(self,request):
print('this is get method!!!')
return render(request, 'login.html')
def post(self,request):
uname = request.POST.get('username')
pwd = request.POST.get('password')
if uname == 'alex' and pwd == 'dsb':
return redirect('/home/')
else:
return redirect('/login/')
The request object
request is accepted view function or method parameter, which encapsulates all the information relating to the request, the most common data are:
request.path #request.path当前请求路径
request.method #当前请求方法(get,post...)
request.GET # 获取所有get请求携带过来的数据
request.POST # 获取所有post请求携带过来的数据
request.body # 获取所有post请求携带过来的数据的原始格式
response object
Each view the return value of a function or method must be a response object that currently we are exposed to are:
- Reply to render html page
- redirect redirect
- HttpResponse reply string