Background: I need to do a POST request to add data to my program.
I'm currently using advanced REST client to simulate the data packet that will be send by a arduino. it is currently sending this as a POST request:
{"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}
if i post this to this class (csrf is disabled)
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;
Then i get the error.
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
I am a noob on python so please give me an extended explanation about whats wrong.
EDIT: i replaced the clashing name of function and class. I am also the wrong way to create JSON strings because I don't have the knowledge to create a JSON string with the correct code.
I also still get the internal error 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;
EDIT: I fixed it with the help of @MosesKoledoye reply. The answer was in the Voltage.
i changed my code to
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;
and then in urls.py i changed the Voltage to voltage
url(r'^voltage/', views.Voltage, name='Voltage'),
to
url(r'^voltage/', views.voltage, name='Voltage'),
thank you for your help, I get an 200 OK as it is supposed to be. thank you for all the responses.
You should not declare a global variable. This is not the right way to create objects in Django.
Instead, just initialise and declare the object after the data is ready to be stored.
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()
Refer to documentation: https://docs.djangoproject.com/en/1.10/ref/models/instances/#creating-objects
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.