我在django中有一个后端,而终端(rest框架)登录。Cordova和Django登录,会话过期
简单的Ajax
$.ajax({
type : "POST",
url : url+login/",
data : {username:username, password:password}
})
和简单的查看
@api_view(['POST'])
def login(request):
username = request.POST.get('username')
password = request.POST.get('password')
user = auth.authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth.login(request, user)
#When I type here: print request.session.items()
#I get _auth_user... things.
else:
pass
return Response({})
但是,当我在我的本机应用程序更改网页,而且调用另一个AJAX例如网址 “测试/”,而这个URL调用这个观点:
def test(request):
if request.user.is_authenticated():
print "Logged in
else:
#redirect to home...
return response
然后request.user.is_authenticated返回False,貌似会话过期是小的,所以我试试这个:
...
auth.login(request, user)
#When I type here: print request.session.items()
#I get _auth_user... things.
request.session.set_expire(0)
...
但这不起作用。编辑** 我使用Django Rest Framework。和“打开”:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
)
}
您没有告诉我们您的休息框架使用了什么。你能确认是否是Django-rest-framework? – dkarchmer
是的,Django-rest-framework – Jeroj82