django3- view function Advanced

1. Function Classification view

  FBV(fucntion base view)

  CBV (class base view), CBV The method names defined, what determines what function execution request

 

2.FBV conversion CBV (not quite right)

  Perform specified class name .as_view () method in the CBV of the url, according to the source that dispatch function execution method in a class of our custom!

####################FBV
def
pressedit(request, edit_id, test_id): obj = models.presslist.objects.get(pk=edit_id) print(test_id, edit_id) msg = '' if request.method == 'POST': obj.name = request.POST.get('pressname') if models.presslist.objects.filter(name=obj.name): msg = '已存在' if not obj.name: msg = '不能为空' if not models.presslist.objects.filter(name=obj.name) and obj.name: obj.save() return redirect(reverse('presslist')) return render(request, 'cbsadd-edit.html', {'obj': obj, 'msg': msg}) #######################CBV class pressedit1(View): def get(self, request, edit_id, test_id): obj = models.presslist.objects.get(pk=edit_id) msg = '' print(edit_id, test_id) print(id(obj)) return render(request, 'cbsadd-edit.html', {'obj': obj, 'msg': msg}) def post(self, request, edit_id, test_id): msg = '' obj = models.presslist.objects.get(pk=edit_id) obj.name = request.POST.get('pressname') print(id(obj)) if models.presslist.objects.filter(name=obj.name): MSG = ' existing ' IF Not obj.name: MSG = ' can not be empty ' IF Not models.presslist.objects.filter (name = obj.name) and obj.name: obj.save () return the redirect (Reverse ( ' presslist ' )) return the render (Request, ' cbsadd-edit.html ' , { ' obj ' : obj, ' MSG ' : MSG})

 

3. The view function decorator

import time
import functools
from django.utils.decorators import method_decorator
def wapp(func): @functools.wraps(func) def inner(*args, **kwargs): time1 = time.time() ret = func(*args, **kwargs) print(time.time() - time1) return ret return inner

#######FBV
#url
url(r'^press/edit/(?P<edit_id>\d+)/(?P<test_id>\d+)/', views.pressedit, name='pressedit'),
#views
@wapp
def presslist(request):
msg_all = models.presslist.objects.all()
return render(request, 'cbs.html', {'msg': msg_all})


#######CBV
#url
url(r'^press/edit/(?P<edit_id>\d+)/(?P<test_id>\d+)/', views.pressedit1.as_view(), name='pressedit'),

 #views

@method_decorator (wapp, name = 'get ') ### specified by this method_decorator own decorator to decorator and decorate function, can act directly dispatch function, valid for all function 
class pressedit1 (View):

DEF GET (Self , Request, edit_id, test_id, MSG = ''):
obj = models.presslist.objects.get (PK = edit_id)
return the render (Request, 'cbsadd-edit.html', { 'obj': obj, 'MSG' : MSG})

DEF POST (Self, Request, edit_id, test_id):
MSG = ''
obj = models.presslist.objects.get (PK = edit_id)
obj.name = request.POST.get ( 'pressname')
IF Models .presslist.objects.filter (name = obj.name):
MSG = 'existing'
IF not obj.name:
MSG = 'can not be empty'
if not models.presslist.objects.filter(name=obj.name) and obj.name:
obj.save()
return redirect(reverse('presslist'))
return self.get(request, edit_id, test_id, msg)

 

Guess you like

Origin www.cnblogs.com/quguanwen/p/11391327.html