django实现注册 登录 ajax

#coding=utf-8
from django.http import HttpResponse
from django.shortcuts import render_to_response
from models import Account
import datetime,json,pdb


def hello(request):
    return HttpResponse("Hello world.")


def hello2(request):
    return HttpResponse("Hello world2.")


def currentTime(request):
    now = datetime.datetime.now()
    return HttpResponse(now)


def login(request):
    return render_to_response('login.html')




def loginCheck(request):
    username=request.POST['username']
    password=request.POST['password']
    #print "username=%s,password=%s" %(username,password)
    try:
        account = Account.objects.get(username=username)
    except:
        return render_to_response('failed.html')
    if account.password != password :
        return render_to_response('failed.html')
    else:
        return render_to_response('success.html')


def ajax_login(request):
    return render_to_response('ajax_login.html')


def ajax_loginCheck(request):
    rsdic={'ret':'1101','info':'ok'}
    username=request.POST['username']
    password=request.POST['password']
    print "username=%s,password=%s" %(username,password)
    #pdb.set_trace()
    try:
        account = Account.objects.filter(username=username)
        if len(account)==0:
            rsdic['ret']='1103'
            rsdic['info']='用户名不存在!'
        if account[0].password != password :
            rsdic={'ret':'1104','info':'密码不正确!'}
        else:
            request.session['username'] = username
            rsdic={'ret':'1101','info':'ok','username':username}
    except Exception,e:
        rsdic={'ret':'1105','info':'用户名不存在!'}
    finally:
        return HttpResponse(json.dumps(rsdic))


def toRegister(request):
     return render_to_response('register.html')


def ajax_register(request):
    print("qq")
    rsdic={'ret':'1101','info':'ok'}
    username=request.POST['username']
    password=request.POST['password']
    confirmPwd=request.POST['confirmPwd']
    #pdb.set_trace()
    try:
        account = Account.objects.filter(username=username)
        if len(account)>0:
             rsdic={'ret':'1106','info':'用户名已经存在'}
        if password!=confirmPwd :
            rsdic={'ret':'1104','info':'两次输入的密码不一致!'}
        else:
            if(rsdic['ret']=='1101'):
                temp = Account()
                temp.username = username
                temp.password = password
                temp.save()
        print rsdic['info']


    except Exception,e:
        rsdic={'ret':'1105','info':'未知的错误!'}
    finally:
        return HttpResponse(json.dumps(rsdic))

你可能感兴趣的:(python,django,ajax)