在__init__.py导入pymysql
?在setting.py文件中连接自己的MySQL数据库
?填写上?ALLOWED_HOSTS =[‘*’]可以使所有的网址都能访问Django项目
设置路由
一级路由
from django.contrib import admin
from django.urls import path, include
from app01 import urls as app01_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app01.urls')),
path('app01/', include(app01_urls, namespace='app01')),
]
二级路由
path('Appreap1/', Appreap1.as_view()),
在models.py中建表
?在终端执行表迁移
-
python manage.py makemigrations -
python manage.py migrate
?在views.py中写函数
class Appreap1(APIView):
def post(self, request):
result = Employee.objects.filter(FIRST_NAME='1')
arr = []
for i in result:
content = {'姓': i.FIRST_NAME, "名": i.LAST_NAME, '年龄': i.AGE, '性别': i.sex, '收入': i.INCOME}
arr.append(content)
print(arr)
FIRST_NAME = request.data.get('FIRST_NAME')
print(FIRST_NAME)
return HttpResponse(arr)
前端hml代码
<div id="wrapper">
<div id="div1">
<div id="div2">
<div id="div3"></div>
<div id="div4">
<text id="text3">HelloWorld</text>
<button id="btn" onclick="onClick">请求响应</button>
</div>
<div class="back">
<text>{{winfo}}</text>
</div>
</div>
</div>
</div>
前端js代码
import qs from 'querystring'
import fetch from '@system.fetch'
export default {
data:{
winfo:"",
},
onClick(){
fetch.fetch({
url:'http://127.0.0.1:8000/app01/Appreap1/',
data:qs.stringify({
'FIRST_NAME':'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+resp)
}
});
},
}
'querystring'会报错,需要安装配置node.js和nmp
在前端的终端执行npm install qs 和 npm install querystring
启动django
再运行前端
结果
?
|