前端配置
1.配置chonfig.json
"default": {
"process": "com.huawei.hiworld.example",
"supportBackup": false,
"network": {
"cleartextTraffic": true
}
}
2.配置module
"reqPermissions":[
{
"name":"ohos.permission.GET_NETWORK_INFO"
},
{
"name":"ohos.permission.SET_NETWORK_INFO"
},
{
"name":"ohos.permission.INTERNET"
},
],
"package":"com.example.testdemo4"
"name":".MyApplication",
"mainAbility":"com.example.testdemo4.MainAbility",
"deviceType":[
"phone",
"tablet",
"tv",
"wearable"
],
3.安装qs
npm install qs
4.j配置s代码
import fetch from '@system.fetch';
import qs from 'querystring';
export default{
data:{
winfo:""
},
onClick(){
fetch.fetch({
url:'http://127.0.0.1:8000/app01/table',
data:qs.stringify({'id':'1'}),
responseType:"json",
method:"POST",
success:(resp)=>
{
this.winfo = resp.data;
console.log("返回的数据: " + this.winfo)
},
fail:(resp)=>
{
this.winfo = resp.data;
console.log("获取数据失败 " + this.winfo)
},
})
}
}
5.编写html
<div class = "container">
<text>点击按钮显示调用内容<text>
<button class = "button1" onclick = "onClick">
按钮
<button>
<text>{{winfo}}<text>
<div>
注:npm报错可按解决“npm不是内部或外部命令“ - 天天向上吧 - 博客园解决
出现这种报错是因为权限不够,需要以管理员身份启动
后端配置
1.python连接数据库
2.创建APP
3.迁移数据库
建表:
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=32)
sex = models.CharField(max_length=32)
school = models.CharField(max_length=32)
迁移:
python manage.py makemigrations app名称 # 让 Django 知道模型有变更
python manage.py migrate app名称 # 创建表结构
4.配置views.py
import pymysql
from django.http import HttpResponse
from rest_framework.views import APIView
from abcd import models
from abcd.models import student
try:
con = pymysql.connect(host='127.0.0.1',
port=3306,
user='root',
password='121121',
db='testdb1',
charset='utf8')
print("数据库连接成功")
expect pymysql.Error as e:
print('数据库连接失败'+str(e))
class AppReq1(APIView):
def post(self,request):
result = student.objects.filter(name='lishuaiqi')
arr = []
for i in result:
content = {'姓名':i.name,'年龄':i.age,'性别':i.sex,'学校':i.school}
arr.append(contend)
print(arr)
print(type(arr))
id = request.data.get('id');
print(id)
return HttpResponse(arr)
运行后端
python manage.py runserver 127.0.0.1:8000
报错则注意端口是否被占用
运行前端