![图片[1]-python怎么判断用户是否登录?-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/5ec37a5f2ed5b627.jpg)
python中判断用户是否登录的方法:
defcmdbindex(req):
ifnotrequest.user.is_authenticated():
returnrender(request,'login_error.html')
else:
returnrender_to_response('cmdb/index.html')
#会跳转到错误页面
defcmdbindex(req):
returnrender_to_response('cmdb/index.html')
requestuser属性:
一个django.contrib.auth.models.User对象表示当前登录用户。若当前用户尚未登录,user会设为django.contrib.auth.models.AnonymousUser的一个实例。
可以将它们与is_authenticated()区别开:
ifrequest.user.is_authenticated(): #Dosomethingforlogged-inusers. else: #Dosomethingforanonymoususers.
user仅当Django激活AuthenticationMiddleware时有效。
原文来自:https://www.py.cn© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END


















































暂无评论内容