注册登录的权限与认证
如何使用django中自带的方法来进行权限认证?
cookie,session和token的原理,都是在什么地方使用它们?

关于#注册登录#的问题,如何解决?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- yuxiaqiao 2022-06-14 21:25关注
给我经常写的示例:
def login(request):
if request.user.is_authenticated:
return redirect(reverse('index'))
if request.method == 'POST':
username = request.POST.get('username', 'not set')
password = request.POST.get('password', 'not set')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_staff or user.is_superuser:
return success_api("登录成功!")
else:
return fail_api("用户名或密码错误!")
return render(request, "login.html", context={})cookie是客户端的
session是服务器端,需要结合cookie
token是一种通行证机制的,可以永久通行证,也可以临时通行证,这个基于安全和性能,结合实际应用场景平衡使用,没有必要教条本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报