Python第31天 CBV登录验证

文章目录

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')

给TA打赏
共{{data.count}}人
人已打赏
开发

Python第50天 Bootstrap模板使用1

2023-9-11 18:33:26

开发

Python第四天 字符串元组列表

2023-9-11 18:33:58

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索