文章目录
2019-1-3 08:46:19
CBV登录验证
- Cookie验证
#urls.py
from django.conf.urls import url,include
from django.contrib import admin
from host import views
urlpatterns = [
url(r'^hostpage/',views.hostpage),
url(r'^add/',views.add),
url(r'^update/',views.update),
url(r'^delete/',views.delete),
url(r'^search/',views.search),
# url(r'',views.login),
url(r'^login/',views.login),
url(r'^logout/',views.logout),
url(r'^register/',views.register),
url(r'^order/',views.Order.as_view()),
]
-------------------------------------------------------------
#views.py
def auth(func):
def inner(request,*args,**kwargs):
v = request.COOKIES.get('username')
if not v:
return redirect('/login/')
return func(request,*args,**kwargs)
return inner
#CBV
@method_decorator(auth,name='dispatch')
class Order(views.View):
def get(self,request):
v = request.COOKIES.get('username')
return render(request,'hostpage.html',locals())
def post(self,request):
v = request.COOKIES.get('username')
return render(request, 'hostpage.html', locals())
def logout(request):
request.COOKIES.clear()
return redirect('/login')