登录注册功能实现Django(一)
- 路由配置
- 注册
- 登录
今天开始进行算法接口的实现,遇到了一些问题,模型在与java通过socket交互时,函数控制台输出可行,可是嵌入jsp会出现很多问题,当前现有的采用jsp部署深度学习的资料很少,没有找到解决方式,于是换用了Django进行部署,下面介绍Django注册登录方法。
路由配置
url(r\'^register$\',views.register,name=\"register\"),url(r\'^save$\',views.save,name=\'save\'),url(r\'^login$\',views.login,name=\"login\"),url(r\'^searchu$\',views.searchu,name=\'searchu\'),
注册
Django环境的搭建及工程创建不做赘述,下面为注册函数:
def save(request):isregistered=0a=request.GETsid=a.get(\'sid\')sname=a.get(\'sname\')snum=a.get(\'snum\')sclass=a.get(\'sclass\')pwd=a.get(\'pwd\')#连接数据库db=pymysql.connect(host=\'localhost\',port=3306,user=\'root\',password=\'123456\',database=\'correct\',charset=\'utf8\')cursor=db.cursor()sql1=\'select * from user\'cursor.execute(sql1)allusers=cursor.fetchall()i=0while i<len(allusers):if sid in allusers[i]:isregistered=1i+=1if isregistered==0:sql2=\'insert into user(sid,sname,snum,sclass,pwd) values (%s,%s,%s,%s,%s)\'cursor.execute(sql2,(sid,sname,snum,sclass,pwd))db.commit()cursor.close()return HttpResponse(\'注册成功\')else:cursor.close()db.close()return HttpResponse(\"该学号已注册\")
登录
登录函数如下:
def searchu(request):a=request.GETsid=a.get(\'sid\')pwd=a.get(\'pwd\')#user=(sid,pwd)db=pymysql.connect(host=\'localhost\',port=3306,user=\'root\',password=\'123456\',database=\'correct\',charset=\'utf8\')cursor=db.cursor()sql1=\'select * from user\'cursor.execute(sql1)allusers=cursor.fetchall()i=0success=0currentuser=[]while i<len(allusers):if sid==allusers[i][0] and pwd==allusers[i][-1]:success=1currentuser=allusers[i]breaki+=1if success==1:print(currentuser)return HttpResponse(\"登录成功\")else:return HttpResponse(\'用户名或密码错误\')
二次修正,登录成功以后要以session形式保存当前用户信息:
if success==1:request.session[\'is_login\'] = Truerequest.session[\'user_id\'] = sidrequest.session[\'user_name\'] = snamerequest.session[\'user_class\'] = sclassprint(currentuser)return render(request,\'index.html\')