Use Django If you need to operate the existing database , Model mapping is required first
The specific operation is as follows
# CMD operation , Enter the project file
python manage.py inspectdb
# Wait for the mapping to complete
python manage.py inspectdb > models.py
# After the manage.py Generate new models.py file , Just copy and paste
Similarly, we need to start with urls.py
Use in as_view()
Method to configure the route , The specific reasons have been explained in the previous chapter
# Return the latest data in the database
class Getlastdata(APIView):
def post(self, request):
try:
patientname = request.data.get('patientname') # Get the front-end transmission data
print(' The user enters a name :' + patientname)
result = models.Patient.objects.filter(name=patientname).last() # Query the latest data that meets the conditions
name = result.name
age = result.age
height = result.height
weight = result.weight
body = result.body
alldata = []
alldata.append({
'name': name, 'age': age, 'height': height, 'weight': weight, 'body': body
})
alldata_json = json.dumps(alldata, ensure_ascii=False) # Convert to JSON Format
return HttpResponse(alldata_json)
except Patient.DoesNotExist as e:
print(' The username does not exist ')
return HttpResponse(' The username does not exist ')
else:
return HttpResponse(' request was aborted ')
# get.js
import fetch from '@system.fetch';
import qs from 'querystring';
import router from '@system.router';
export default{
data:
{
winfo:'',
detail:[{
// Write initial value
name:'aaa',
age :'18',
height:'180',
weight:'70',
body:' Leg '
}]
},
onClick(){
fetch.fetch({
url:`http://127.0.0.1:8000/Qingtian/getlastdata/`,
data:qs.stringify({
patientname:'bbb'}), // Request to transmit back-end data
responseType:'json',
method:'POST',
success:(resp)=>
{
var getdata
getdata = JSON.parse(resp.data) // Back end data front end assignment
this.detail[0].name = getdata[0].name
this.detail[0].age = getdata[0].age
this.detail[0].height = getdata[0].height
this.detail[0].weight = getdata[0].weight
this.detail[0].body = getdata[0].body
this.winfo = resp.data
console.log(' Return the data :'+this.winfo),
console.log(' data format :'+typeof(this.winfo))
},
fail:(resp)=>
{
console.log(' Failed to get data ')
}
})
}
}