前言
一、DevEco Studio前端
1.登录页面制作(.hml)
2.登录逻辑实现(.js)
二、django后端
1.实现数据库的连接
2.login功能实现
总结
问题
前言
通过django与DevEco Studio的连接,实现登录功能
一、DevEco Studio前端
1.登录页面制作(.hml)
{{winfo}}
2.登录逻辑实现(.js)
import fetch from '@system.fetch';
import router from '@system.router';
import qs from 'querystring';
export default{
data:{
fit:"cover",
winfo:"",
},
docRegist(){
router.push({
uri:'pages/index/index',
})
},
inputAccount(e){
this.username = e.value;
},
inputPassword(e){
this.password = e.value;
},
onClick(){
fetch.fetch({
url:'http://127.0.0.1:8000/Person/login/',
data:qs.stringify({'username':'111','password':'111'}),
responseType:"json",
method:"POST",
success:(resp)=>
{
this.winfo = resp.data
console.log("返回数据:"+this.winfo)
if(this.winfo=="登录成功"){
router.push({
uri:'pages/index/index',
})
}
},
fail:(resp)=>
{
this.winfo = resp.data;
console.log("获取数据失败:"+this.winfo)
}
});
}
}
二、django后端
1.实现数据库的连接
{{winfo}}
2.登录逻辑实现(.js)
import fetch from '@system.fetch';
import router from '@system.router';
import qs from 'querystring';
export default{
data:{
fit:"cover",
winfo:"",
},
docRegist(){
router.push({
uri:'pages/index/index',
})
},
inputAccount(e){
this.username = e.value;
},
inputPassword(e){
this.password = e.value;
},
onClick(){
fetch.fetch({
url:'http://127.0.0.1:8000/Person/login/',
data:qs.stringify({'username':'111','password':'111'}),
responseType:"json",
method:"POST",
success:(resp)=>
{
this.winfo = resp.data
console.log("返回数据:"+this.winfo)
if(this.winfo=="登录成功"){
router.push({
uri:'pages/index/index',
})
}
},
fail:(resp)=>
{
this.winfo = resp.data;
console.log("获取数据失败:"+this.winfo)
}
});
}
}
二、django后端
1.实现数据库的连接
1.实现数据库的连接
在views下进行书写
try:
con = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='123456',
db='shixun')
print('数据库连接成功')
except pymysql.Error as e:
print('数据库连接失败'+str(e))
2.login功能实现
不要忘了在urls文件中添加login
class login(APIView):
def post(self,request):
username = request.data.get("username")#从前端获取数据
password = request.data.get("password")
print(username)
cur = con.cursor()
sql = "select * from login where name=%s"#sql语句,待补全
values = (username)#补全sql语句
try:
if cur.execute(sql,values):#补全sql语句,并判断是否存在
con.commit()
results = cur.fetchall()
for row in results:#将从数据库获取到的数据赋给变量
Pusername = row[1]
Ppassword = row[2]
print(Pusername)
if password == Ppassword:#判断前端和数据库的密码是否相同
print('登录 成功')
return HttpResponse('登录成功')
else:
print('查无此人')
except pymysql.Error as e:
print('查无此人'+str(e))
return HttpResponse('请求失败')
总结
前端传数据,后端在得到数据后,对数据库进行查询,进一步比较密码,确定结果后回传到前端,进行下一步的操作。
问题
对于GET和POST不过理解,写method全凭运气去写



