我正在用Django编写网络代码。我不能使用会话。我想检查登录使用会话
def user_login(request):
if request.session['is_login'] == True:
return HttpResponseRedirect('/home')
else:
next = request.POST.get('next', request.GET.get('next', ''))
if request.method == "POST":
username1 = request.POST.get('username')
password1 = request.POST.get('password')
user = authenticate(username=username1, password=password1)
if user is not None:
if user.is_active:
login(request, user)
if next:
request.session['is_login'] = True
return HttpResponseRedirect(next)
return HttpResponseRedirect('/home')
else:
return HttpResponse('Inactive user')
else:
return HttpResponseRedirect(LOGIN_URL)
return render(request, "pages/login.html")
如何修复
答案 0 :(得分:1)
我在评论中提到
更改此内容:
public static int[][] mystry2d(int[][] a){
for(int r = 0; r<a.length; r++){
for(int c=0; c<a.length-1;c++){
if(a[r][c+1] > a[r][c]){
a[r][c] = a[r][c+1] ;
}
}
}
return a ;
}
对此:
public static void printArray(int[][] arr){
for (int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]);
}
System.out.println();
}
}
**input:** int[][] numbers= {{3,4,5,6},{4,5,6,7},{5,6,7,8}};
**output:** 4 5 6 6
5 6 7 7
6 7 8 8
不引发 4 5 5 6
5 6 6 7
6 7 7 8
,它返回键位置的值,或者返回if request.session['is_login'] == True:
。
您也可以在找不到密钥时更改默认行为,即可以返回以下内容代替返回if request.session.get('is_login') == True:
:
request.session.get
上面的语句在失败时将返回KeyError
而不是None
。
访问docs了解更多信息。
答案 1 :(得分:0)
这里是request.session没有一个名为is_login的密钥,最初您是在用户成功登录时插入该密钥的,因此您需要添加一个检查密钥是否存在于会话字典中
使用此
if 'is_login' in request.session and request.session['is_login'] == True:
return HttpResponseRedirect('/home')
答案 2 :(得分:0)
我认为您在进行错误检查以查看用户是否已通过身份验证。如果要查看用户是否已通过身份验证,只需使用is_authenticated:
def user_login(request):
if request.user.is_authenticated:
return HttpResponseRedirect('/home')
else:
# rest of the code
还请删除行request.session['is_login'] = True
,因为django会自行处理身份验证。您无需使用会话来存储该信息。
答案 3 :(得分:0)
谢谢大家的支持。我解决了 如果request.session.get ['is_login'] == True,我做了编辑 如果request.session.get('is_login')==真: