背景:我需要发一个POST请求来向我的程序添加数据。
我目前正在使用高级REST客户端来模拟将由arduino发送的数据包。 它目前正在发送这个POST请求:
{"cellVoltageLow":28,"cellVoltageHigh":415,"cellVoltageAvg":415,
"cellTempMin":5,"cellTempMax":15,"cellTempAvg":10,"stateOfCharge":50,
"errorBitmap":0,"batteryVoltageIn":420,"motorAmp":200,"usageAmp":210,
"auxAmp":10,"rpmMotor":1000,"uiState":0,"chargingStatus":1,
"motorTemp":15,"inverterTemp":30,"inletTemp":5,"ambientTemp":5,
"chargingAmp":10,"chargingVoltage":420,"apsVoltage":42,
"maxAmpsInCharge":10,"bmsErrorValue":0,"energyDriveAH":1000,
"energyRegenAH":100,"energyChargeAH":10,"energyDischargeAH":10,
"energyDriveWH":1000,"energyRegenWH":10,"energyChargeWH":2300,
"energyDischargeWH":23000,"drivenDistanceCM":100000,
"chargedEnergyWH":1000,"returnedEnergyWH":10000,
"connectedTimeSec":100,"driveTimeSec":240}
如果我将此帖子发布到此类(禁用csrf)
def Voltage(request):
global Voltage
if request.method == 'POST':
data = json.loads(request.body.decode('utf-8'))
Voltage.cellVoltageLow = data["cellVoltageLow"]
Voltage.cellVoltageAvg = data["cellVoltageAvg"]
Voltage.cellVoltageHigh = data["cellVoltageHigh"]
Voltage.batteryVoltageIn = data["batteryVoltageIn"]
Voltage.chargingVoltage = data["chargingVoltage"]
Voltage.apsVoltage = data["apsVoltage"]
print("got ur message" + str(request.body))
return HttpResponse("OK")
if request.method == 'GET':
return HttpResponse('{ "cellVoltageLow":' + str(Voltage.cellVoltageLow)
+ '{ "cellVoltageAvg":' + str(Voltage.cellVoltageAvg)
+ '{ "cellVoltageHigh":' + str(Voltage.cellVoltageHigh)
+ '{ "batteryVoltageIn":' + str(Voltage.batteryVoltageIn)
+ '{ "chargingVoltage":' + str(Voltage.chargingVoltage)
+ '{ "apsVoltage":' + str(Voltage.apsVoltage)
+ ' }' )
else:
return HttpResponse ('Not allowed')
class Voltage:
cellVoltageLow = 0;
cellVoltageAvg = 0;
cellVoltageHigh = 0;
batteryVoltageIn = 0;
chargingVoltage = 0;
apsVoltage = 0;
然后我得到了错误。
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
response = get_response(request)
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: object() takes no parameters
我是python上的菜鸟,所以请给我一个关于错误的扩展解释。
编辑: 我替换了函数和类的冲突名称。我也是创建JSON字符串的错误方法,因为我没有使用正确的代码创建JSON字符串的知识。
我还是得到内部错误500
def voltage(request):
global voltage
if request.method == 'POST':
data = json.loads(request.body.decode('utf-8'))
Voltage.cellVoltageLow = data["cellVoltageLow"]
Voltage.cellVoltageAvg = data["cellVoltageAvg"]
Voltage.cellVoltageHigh = data["cellVoltageHigh"]
Voltage.batteryVoltageIn = data["batteryVoltageIn"]
Voltage.chargingVoltage = data["chargingVoltage"]
Voltage.apsVoltage = data["apsVoltage"]
print("got ur message" + str(request.body))
return HttpResponse("OK")
if request.method == 'GET':
return HttpResponse('{ "cellVoltageLow":' + str(Voltage.cellVoltageLow)
+ '{ "cellVoltageAvg":' + str(Voltage.cellVoltageAvg)
+ '{ "cellVoltageHigh":' + str(Voltage.cellVoltageHigh)
+ '{ "batteryVoltageIn":' + str(Voltage.batteryVoltageIn)
+ '{ "chargingVoltage":' + str(Voltage.chargingVoltage)
+ '{ "apsVoltage":' + str(Voltage.apsVoltage)
+ ' }' )
else:
return HttpResponse ('Not allowed')
class Voltage:
cellVoltageLow = 0;
cellVoltageAvg = 0;
cellVoltageHigh = 0;
batteryVoltageIn = 0;
chargingVoltage = 0;
apsVoltage = 0;
编辑:我在@MosesKoledoye回复的帮助下修复了它。答案是电压。
我将代码更改为
def voltage(request):
global voltage
if request.method == 'POST':
data = json.loads(request.body.decode('utf-8'))
Voltage.cellVoltageLow = data["cellVoltageLow"]
Voltage.cellVoltageAvg = data["cellVoltageAvg"]
Voltage.cellVoltageHigh = data["cellVoltageHigh"]
Voltage.batteryVoltageIn = data["batteryVoltageIn"]
Voltage.chargingVoltage = data["chargingVoltage"]
Voltage.apsVoltage = data["apsVoltage"]
print("got ur message" + str(request.body))
return HttpResponse("OK")
if request.method == 'GET':
return HttpResponse('{ "cellVoltageLow":' + str(Voltage.cellVoltageLow)
+ '{ "cellVoltageAvg":' + str(Voltage.cellVoltageAvg)
+ '{ "cellVoltageHigh":' + str(Voltage.cellVoltageHigh)
+ '{ "batteryVoltageIn":' + str(Voltage.batteryVoltageIn)
+ '{ "chargingVoltage":' + str(Voltage.chargingVoltage)
+ '{ "apsVoltage":' + str(Voltage.apsVoltage)
+ ' }' )
else:
return HttpResponse ('Not allowed')
class Voltage:
cellVoltageLow = 0;
cellVoltageAvg = 0;
cellVoltageHigh = 0;
batteryVoltageIn = 0;
chargingVoltage = 0;
apsVoltage = 0;
然后在urls.py中我将电压更改为电压
url(r'^voltage/', views.Voltage, name='Voltage'),
to
url(r'^voltage/', views.voltage, name='Voltage'),
谢谢你的帮助,我得到了200 OK,因为它应该是。 谢谢你的所有回复。
答案 0 :(得分:0)
您不应声明全局变量。这不是在Django中创建对象的正确方法。
相反,只需在数据准备好存储后初始化并声明对象。
if request.method == "POST":
data = json.loads(request.body.decode('utf-8'))
new_voltage = Voltage.objects.create()
new_voltage.cellVoltageLow = data["cellVoltageLow"]
... # Store other data too
new_voltage.save()
请参阅文档:https://docs.djangoproject.com/en/1.10/ref/models/instances/#creating-objects