views:

47

answers:

2

Hi,

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/$', '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);
 });
+1  A: 

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 POSTing the login information and using HTTPS for security reasons, but that's a different issue.

sdolan
A: 

sorry, the url correct is :

r'^login/$', 'autenticacao.views.login_view'

but it instill does not work yet!

Michel Andrade
Can you see that you're successfully logging in? Try adding adding `import pdb;pdb.set_trace()` after `login(request, user)`, or even just `raise` an exception.
sdolan