如何在 Django 中更新文本区域内容

时间:2021-06-02 11:18:45

标签: python django

我正在尝试使用来自扫描条形码并记录代码的服务的数据刷新文本区域。不知道哪里做错了,我无法让扫描的数据出现在文本区域。

这是我目前所做的

我的相机流,用于扫描条形码并记录到控制台和列表 used_codes 变量

class CameraStream:
    used_codes = []
    def __init__(self):
        self.camera = cv2.VideoCapture(int(0))


    def get_frames(self):
        used_codes = self.used_codes
        while True:
            # Capture frame-by-frame
            success, frame = self.camera.read()
            if not success:
                break
            else:
                ret, buffer = cv2.imencode('.jpg', frame)

                # Add text on top of the barcode if there is a barcode in the stream using opencv
                # convert camera frame to numpy array
                color_image = np.asanyarray(frame)


                # decode numpy array to check if there is a barcode in color_image
                if decode(color_image):
                    for code in decode(color_image):
                        if code.data.decode('utf-8') not in used_codes:
                            print('Approved. You can enter!')
                            print(code.data.decode('utf-8'))
                            used_codes.append(code.data.decode('utf-8'))
                        elif code.data.decode('utf-8') in used_codes:
                            print('Sorry, this code has been already used!')
                        else:
                            pass
                        cv2.imshow('Testing-code-scan', frame)
                        cv2.waitKey(1)
 return used_codes

我的视图从 used_codes 变量中获取数据并将其发送到视图

def detect(request):
    stream = CameraStream()
    success, frame = stream.camera.read()
    if success:
        status = True
    else:
        status = False
    bar_codes = stream.used_codes
    print(bar_codes)
    return render(request, 'detect_barcodes/detect.html', context={'bar_codes': bar_codes, 'cam_status': status})

我的 HTML

  <div class="container">
   <form action = "/cgi-bin/hello_get.cgi" method = "get" id="myForm">
         Fill the Detail:
         <br />

         <textarea rows = "5" cols = "50" name = "description">
            {{ bar_codes }}
         </textarea>

         <input type = "submit" value = "submit" />
      </form>
  </div>

我怎样才能让它在每次扫描代码时都会实时出现在文本区域,就像它登录到控制台的方式一样,提前谢谢。

0 个答案:

没有答案
相关问题