如何在 Django 中获取更新的类变量值

Posted

技术标签:

【中文标题】如何在 Django 中获取更新的类变量值【英文标题】:How to get updated class variable value in Django 【发布时间】:2022-01-06 23:53:21 【问题描述】:

我无法获取类变量的更新值。

p_request函数执行后调用ConnectTestAPI时,resultorderNo的类变量应该在post函数中更新。

然后我想通过在p_request函数中循环while语句来接收类变量的更新值。

但是,尽管用post请求设置了类变量的值,但是当while语句运行时,对应的值仍然分别为空和0值,

因此,while 语句无法终止并导致超时错误。 这是我的源代码。提前谢谢!

class ConnectTestAPI(APIView):
   result=""
   orderNo=0
   
   def post(self, request):
      data = request.data
      ConnectTestAPI.result = data['result'] 
      ConnectTestAPI.orderNo = data['orderNo'] 
      print(ConnectTestAPI.result) # I could successfully get data from POST request here!
      print(ConnectTestAPI.orderNo) # I could successfully get data from POST request here!
      return HttpResponse("ok")

   def p_request():
      data = 
         "a" : 1234,
         "b" : 5678
      
      data = json.dumps(data,ensure_ascii=False).encode('utf-8')
      con = redis.StrictRedis(outside_server['ip'],outside_server['port'])
      con.set("data_dict", data)
      while True:
         if ConnectTestAPI.result != "" and ConnectTestAPI.orderNo != 0:
            break
      res_result = ConnectTestAPI.result
      res_orderNo = ConnectTestAPI.orderNo
      return res_result, res_orderNo

【问题讨论】:

【参考方案1】:

您需要使用self访问类变量:

class ConnectTestAPI(APIView):
   result=""
   orderNo=0
   
   def post(self, request):
      data = request.data
      self.result = data['result'] 
      self.orderNo = data['orderNo'] 
      print(self.result) # I could successfully get data from POST request here!
      print(self.orderNo) # I could successfully get data from POST request here!
      return HttpResponse("ok")

   def p_request():
      data = 
         "a" : 1234,
         "b" : 5678
      
      data = json.dumps(data,ensure_ascii=False).encode('utf-8')
      con = redis.StrictRedis(outside_server['ip'],outside_server['port'])
      con.set("data_dict", data)
      while True:
         if self.result != "" and self.orderNo != 0:
            break
      res_result = self.result
      res_orderNo = self.orderNo
      return res_result, res_orderNo

注意:不建议使用这种类属性。您正在改变一个类属性,该属性对该类的所有实例都有副作用。在您的情况下,在 __ init__() 中初始化的普通属性就可以了:

class ConnectTestAPI(APIView):
   def __init__(self):
      self.result=""
      self.orderNo=0
   
   def post(self, request):
      data = request.data
      self.result = data['result'] 
      self.orderNo = data['orderNo'] 
      print(self.result) # I could successfully get data from POST request here!
      print(self.orderNo) # I could successfully get data from POST request here!
      return HttpResponse("ok")

   def p_request():
      data = 
         "a" : 1234,
         "b" : 5678
      
      data = json.dumps(data,ensure_ascii=False).encode('utf-8')
      con = redis.StrictRedis(outside_server['ip'],outside_server['port'])
      con.set("data_dict", data)
      while True:
         if self.result != "" and self.orderNo != 0:
            break
      res_result = self.result
      res_orderNo = self.orderNo
      return res_result, res_orderNo

【讨论】:

感谢您的回答!!我刚刚尝试了您上传的两个答案,但“self.result”和“self.orderNo”仍然是初始值......我该如何解决? 我认为,您需要显示实例化和使用类的代码。上面定义的类就ok了。

以上是关于如何在 Django 中获取更新的类变量值的主要内容,如果未能解决你的问题,请参考以下文章

Django 使用getattr() 方法获取配置文件的变量值

如何在 javascript 代码中更改 django 模板变量值?

Django:如何将python变量值传递给javascript文件?

如何在 SwiftUI 中更新变量值?

将 Django 变量值分配给 AngularJS

Django Templates — 将模板中的变量值传递给 templatetags 过滤器