Django - Start Session by Ajax Request
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 w开发者_运维百科orks! What is going on?
'# urls
r'^logout/$', 'autenticacao.views.logout_view'
'# 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);
});
You're not calling the login_view
. You're ajax request is going to the /logout/
url which is calling the autenticacao.views.logout_view
.
Also, The ?>
after username=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 using HTTPS
for security reasons, but that's a different issue.
精彩评论