Django微信小程序后台 模拟测试和真机测试
小程序本地开发调试时需要手机与电脑处在同一局域网下,并且url要用电脑的局域网ip
1. 获取局域网ip,备用
- cmd–>ipconfig查看地址
2. 微信小程序端
- index.wxml
<!--index.wxml-->
<view class="container">
<input type="text" class="input" bindinput='input'/>
<button bindtap="calculate">cal</button>
<view>{{ result }}</view>
</view>
- index.wxss
/**index.wxss**/
.input {
border: 1px solid black;
margin-bottom: 5px;
}
- index.js
//index.js
//获取应用实例
const app = getApp()
Page({
data: {
result: "暂无结果",
mori: ''
},
//事件处理函数
calculate: function () {
wx.request({
// 此处为第一步获取的ip
url: 'http://192.168.0.105:8000/calculate/',
data: {
mori: this.data.mori
},
success: res => {
if (res.statusCode == 200) {
this.setData({
result: res.data
})
}
}
})
},
input: function (e) {
this.setData({
mori: e.detail.value
})
}
})
- 微信端界面
3. Django端
- 创建项目
django-admin startproject weixintest
- 进入项目
cd weixintest
- 创建App
python manage.py startapp CalculateApi
- 修改calculator/settings.py文件
# 修改 ALLOWED_HOSTS = [] 为
ALLOWED_HOSTS = ['*']
# 注册app
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'CalculateApi'
]
- weixintest/urls.py
# weixintest/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('CalculateApi.urls'))
]
- weixintest/CalculateApi/urls.py
# weixintest/CalculateApi/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('calculate/', views.calculate)
]
- weixintest/CalculateApi/views.py
# weixintest/CalculateApi/views.py
from django.http import HttpResponse
def calculate(request):
formula = request.GET['mori']
print(formula)
try:
result = eval(formula, {})
except:
result = 'Error formula'
return HttpResponse(result)
- 运行Django
# 第一步获取的ip地址
python manage.py runserver 192.168.0.105:8000
- 测试后台
http://192.168.0.105:8000/calculate/?mori=1-3
4. 运行结果
参考文章https://blog.csdn.net/qq_43467898/article/details/83187698