当前位置:   article > 正文

梅科尔工作室——Django+HarmonyOS实现用户登录_harmonyos怎么连接localhost

harmonyos怎么连接localhost

Django+HarmonyOS实现用户登录

前言

使用软件:pycharm专业版、DevEco studio
文章中的mm为简写


一、使用pymysql连接数据库

# connect mysql
try:
    cnt = pymysql.connect(host='localhost',
                          port=3306,
                          user='用户',
                          mm='',
                          db='数据库名称',
                          charset='utf8')
    print('数据库连接成功')
except pymysql.Error as e:
    print('数据库连接失败' + str(e))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

二、后端连接数据库验证

# 登录方法
class login(APIView):
    def post(self, request):
        username = request.data.get('username')     # 获取前端传输的用户名和密码
        password = request.data.get('password')
        # print(username)
        # print(password)
        cur = cnt.cursor()                          # 创建sql游标
        sql = 'select * from login where name=%s'   # sql语句查询指定表指定元素
        values = (username)
        try:
            if cur.execute(sql, values):
                cnt.commit()
                results = cur.fetchall()
                print(results)
                for row in results:                 # 循环调出用户名和密码
                    Pusername = row[1]
                    Ppassword = row[2]
                    # print(Pusername)
                    # print(Ppassword)
                if password in Ppassword:           # 验证
                    print('账号密码验证通过')
                    return HttpResponse('登陆成功')
            else:
                print('查无此人')
                return HttpResponse('查无此人')
        except pymysql.Error as e:
            print('查无此人'+str(e))
            return HttpResponse('请求失败')

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

注意需要在下级urls.py文件中设置路由

因为login方法为类方法,不能直接使用views.login调用,需要使用as_view()方法,这个方法将函数打包在views中,返回符合要求的一个request

注意/符号

from .views import login

urlpatterns = [
    path('login/', login.as_view())	
]
  • 1
  • 2
  • 3
  • 4
  • 5

三、HarmonyOS前端实现

需要注意的几个点主要是HarmonyOSfetch()方法进行网络请求,以及数据格式的解析

# login.js

import fetch from '@system.fetch';
import qs from 'querystring';
import router from '@system.router';

export default{
    data:{
        winfo:""
    },

    inputAccount(e){                    // 获取文本框数据
        this.username = e.value;
    },
    inputPassword(e){
        this.mm = e.value;
    },

    onClick(){
    fetch.fetch({                      // 发送请求
        url:`http://127.0.0.1:8000/目标文件/login/`,
        data: qs.stringify({'username':'this.username', 'mm':'this.mm'}),       // 发送到后端的数据
        responseType:'json',
        method:'POST',
        success:(resp)=>
        {
            this.winfo = resp.data;
            console.log('返回的数据:'+this.winfo)
            if(this.winfo=='登陆成功'){                             // 登录成功后跳转页面
                router.push({
                    uri:'pages/request/request'
                })
            }
        },
        fail:(resp)=>
        {
            this.winfo = resp.data;
            console.log('获取数据失败:' + this.winfo)
        }
    })
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

注意:需在config.js中配置好网络请求

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/274897
推荐阅读
相关标签
  

闽ICP备14008679号