鸿蒙os连接Django后端

鸿蒙前端

1.准备工作

Config.json 配置:
1、
"deviceConfig": {
 "default": {
 "process": "com.huawei.hiworld.example",
 "supportBackup": false,
 "network": {
 "cleartextTraffic": true
 }
 }
},
2、
"reqPermissions": [
 {
 "name": "ohos.permission.GET_NETWORK_INFO"
 },
 {
 "name": "ohos.permission.SET_NETWORK_INFO"
 },
 {
 "name": "ohos.permission.INTERNET"
 }
],

2.代码

index.html

<div class="container">
    <div class="title">
        <text >Django 4</text>
    </div>
    <div>
        <button class="btn" onclick="onclick">
            请求响应
        </button>
    </div>
    <div>
        <text class="txt2">{{winfo}}</text>
    </div>
</div>

index.css

.container{
    background: linear-gradient(120deg, #3498db, #8e44ad);
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
}
.btn{
    width: 200px;
    height: 40px;
    font-size: 20px;
}

index.js

导入qs是会存在报错情况,在终端里输入npm install qs 和 npm install querystring,如不成功需要下载Node.js(Node.js 中文网 (nodejs.cn))

import qs from 'querystring'
import fetch from '@system.fetch';
export default{
    data:{
        winfo:""
    },
    onclick(){
//        发起网络请求
        fetch.fetch({
            url:`http://127.0.0.1:8000/tt/AppRed/`,
            data:qs.stringify({'id':'1'}),
            responseType:"json",
            method:"POST",
            success:(resp)=>
            {
                this.winfo=resp.data;
                console.log("返回数据:"+this.winfo)
             },
            fail:(resp)=>{
                console.log("获取数据失败")
            }
        });
    },
}

 

3.效果

 Django后端

1.准备工作

创建好项目,以及完成mysql数据库配置和迁移工作

2.配置

填写上 ALLOWED_HOSTS =[‘*’]可以使所有的网址都能访问 Django 项目了 项目测试的时候,可以这么做,但是这样就失去了保护

3.部分代码(tt/views.py)

from django.http import HttpResponse
from .models import B
from rest_framework.views import APIView


# Create your views here.

class AppRed(APIView):
    def post(self, request):
        print("连接成功·")
        res = B.objects.filter(id=1)
        arr = []
        for i in res:
            content = {'姓名': i.Name, '年龄': i.Age, '性别': i.Sex}
            arr.append(content)
        print(arr)
        print(type(arr))

        ID = request.data.get('id')
        print(ID)
        return HttpResponse(arr)

4.启动项目

python manage.py runserve

json数据

JSON数据格式(JavaScript Object Notation缩写
定义:是一种轻量级的数据交换格式
特点:
1、易于程序员阅读和编写。
2、易于计算机解析和生成。
3、其实是javascript的子集:原生javascript支持JSON

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值