简体   繁体   English

Django TypeError:object()不带参数

[英]Django TypeError: object() takes no parameters

Background: I need to do a POST request to add data to my program. 背景:我需要执行POST请求以将数据添加到我的程序。

I'm currently using advanced REST client to simulate the data packet that will be send by a arduino. 我目前正在使用高级REST客户端来模拟将由arduino发送的数据包。 it is currently sending this as a POST request: 它目前正在发送这个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}

if i post this to this class (csrf is disabled) 如果我把这个发布到这个类(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;

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. 我是python上的菜鸟,所以请给我一个关于什么错误的扩展解释。

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. 我也是创建JSON字符串的错误方法,因为我没有使用正确的代码创建JSON字符串的知识。

I also still get the internal error 500 我还得到内部错误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. 编辑:我在@MosesKoledoye回复的帮助下修复了它。 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 然后在urls.py中我将电压更改为电压

    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. 谢谢你的帮助,我得到了200 OK,因为它应该是。 thank you for all the responses. 谢谢你的回复。

You should not declare a global variable. 您不应该声明全局变量。 This is not the right way to create objects in Django. 这不是在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 请参阅文档: https//docs.djangoproject.com/en/1.10/ref/models/instances/#creating-objects

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM