Django - 通过 Ajax 请求启动会话
我需要知道如何在 Django 中通过 Ajax 启动会话。我完全按照下面的描述做,但它不起作用!请求已正确发送,但未启动任何会话。如果直接请求而不使用ajax就可以了!到底是怎么回事?
'# urls
r'^logout/$', 'autenticacao.views.logout_view'
'# 登录视图
def login_view(request):
username = request.GET.get('username', '')
password = request.GET.get('password', '')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponse(user.get_profile().sos_user.name)
return HttpResponse('user invalido')
'# html 页面中的 ajax
$(function(){
$.get('http://localhost:8000/logout/?username=usuario?>&password=senha', function(data){
alert(data);
});
I need to know how to start a session by Ajax in Django. I'm doing exactly as described bellow, but it is not working! The request is sent correctly, but don't start any session. If a request directly without ajax it works! What is going on?
'# urls
r'^logout/
'# view of login
def login_view(request):
username = request.GET.get('username', '')
password = request.GET.get('password', '')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponse(user.get_profile().sos_user.name)
return HttpResponse('user invalido')
'# ajax in a html page
$(function(){
$.get('http://localhost:8000/logout/?username=usuario?>&password=senha', function(data){
alert(data);
});
, 'autenticacao.views.logout_view'
'# view of login
'# ajax in a html page
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有调用
login_view
。您的 ajax 请求将发送到调用autenticacao.views.logout_view
的/logout/
url。另外,
username=usuario
之后的?>
在您的获取网址中看起来不正确。我的猜测是你应该做类似
http://localhost:8000/login/?username=usuario&password=senha
的事情。 (但我需要查看您的登录网址映射才能确定)。此外,出于安全原因,您应该
POST
登录信息并使用HTTPS
,但这是另一个问题。You're not calling the
login_view
. You're ajax request is going to the/logout/
url which is calling theautenticacao.views.logout_view
.Also, The
?>
afterusername=usuario
doesn't look right in the your get url.My guess is you should be doing something like
http://localhost:8000/login/?username=usuario&password=senha
. (but I'd need to see your login url mapping to be sure).Also, you should be
POST
ing the login information and usingHTTPS
for security reasons, but that's a different issue.