程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Mecol Studio - django+harmonyos realizes user login

編輯:Python

Django+HarmonyOS Implement user login

Preface

Use software :pycharm pro 、DevEco studio
In the article mm For shorthand


One 、 Use pymysql Connect to database

# connect mysql
try:
cnt = pymysql.connect(host='localhost',
port=3306,
user=' user ',
mm='',
db=' Database name ',
charset='utf8')
print(' Database connection successful ')
except pymysql.Error as e:
print(' Database connection failed ' + str(e))

Two 、 Backend connection database verification

# Login method 
class login(APIView):
def post(self, request):
username = request.data.get('username') # Get the user name and password of the front-end transmission 
password = request.data.get('password')
# print(username)
# print(password)
cur = cnt.cursor() # establish sql The cursor 
sql = 'select * from login where name=%s' # sql Statement query specified table specified element 
values = (username)
try:
if cur.execute(sql, values):
cnt.commit()
results = cur.fetchall()
print(results)
for row in results: # Call up the user name and password 
Pusername = row[1]
Ppassword = row[2]
# print(Pusername)
# print(Ppassword)
if password in Ppassword: # verification 
print(' The account password has been verified ')
return HttpResponse(' Landing successful ')
else:
print(' Check no one ')
return HttpResponse(' Check no one ')
except pymysql.Error as e:
print(' Check no one '+str(e))
return HttpResponse(' request was aborted ')

Pay attention to the need to be at the lower level urls.py Set route in file

because login Methods are class methods , Can't be used directly views.login call , Need to use as_view() Method , This method packages functions in views in , Return one that meets the requirements request

Be careful / Symbol

from .views import login
urlpatterns = [
path('login/', login.as_view())
]

3、 ... and 、HarmonyOS The front-end implementation

The main points that need attention are HarmonyOS Of fetch() Method to make a network request , And the analysis of data format

# login.js
import fetch from '@system.fetch';
import qs from 'querystring';
import router from '@system.router';
export default{

data:{

winfo:""
},
inputAccount(e){
 // Get text box data 
this.username = e.value;
},
inputPassword(e){

this.mm = e.value;
},
onClick(){

fetch.fetch({
 // Send a request 
url:`http://127.0.0.1:8000/ Target file /login/`,
data: qs.stringify({
'username':'this.username', 'mm':'this.mm'}), // Data sent to the backend 
responseType:'json',
method:'POST',
success:(resp)=>
{

this.winfo = resp.data;
console.log(' Returned data :'+this.winfo)
if(this.winfo==' Landing successful '){
 // After logging in successfully, jump to the page 
router.push({

uri:'pages/request/request'
})
}
},
fail:(resp)=>
{

this.winfo = resp.data;
console.log(' Failed to get data :' + this.winfo)
}
})
}
}

Be careful : Need to be in config.js Configure network requests in


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved