Preface
Use software :pycharm pro 、DevEco studio
In the article mm For shorthand
# 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))
# 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())
]
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