开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜