无法将我的输出从 views.py 发送到 django 中的模板

Cannot send my output from views.py to template in django

我的应用程序所做的是捕获实时图像并使用 google 视觉 ocr 提取一些值。 我从外部 python 脚本获取数据,我想在 html 文本字段中显示输出。

这是我的 views.py 模板

from django.shortcuts import render, render_to_response
from django.http import HttpResponse
from django.http import JsonResponse
from .models import TemperatureRecordsF, TemperatureRecordC , BloodPressureRecord , SPO2Levels
import sys
import base64
from PIL import Image
from subprocess import run, PIPE


def button(request, *args, **kwargs):
    
    return render(request,'capturr1.html',)

def external(request, *args, **kwargs):

    request_getdata = request.POST.get('img64',None)
    selected_radio = request.POST.get('radio_seleect',None)
    print(selected_radio)

    # print(request_getdata)
   
    headed, encoded = request_getdata.split(",",1)
    data = base64.b64decode(encoded)
    
    
    with open("D:/dev/django/src/OCR/media/image.png", "wb") as f:
        f.write(data)
    path = 'D:/dev/django/src/OCR/media/image.png'
    out = run([sys.executable,'D://dev//django//src//OCR//detect_text.py',path],shell=False,stdout=PIPE)
    print(out.stdout)

    context = {
        'output' : out.stdout
    }
    
    return render(request,'capturr1.html',context)

这是我的模板capturr1.html

<form class="contentarea" action="/external" method="POST" id="capture" name="capture">
    {% csrf_token %}
    <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1">
    <label class="form-check-label" for="inlineRadio1">Temperature °F</label>
    <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2">
    <label class="form-check-label" for="inlineRadio2">Temperature °C</label>
    <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3">
    <label class="form-check-label" for="inlineRadio3">Blood Pressure</label>
    <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio4" value="option4">
    <label class="form-check-label" for="inlineRadio4">Oxygen Saturation Levels</label><br>
    <video id="video">Video stream not available.</video><br>
    <button id="startbutton">Take photo</button>
    <input type="submit" value="Save" class="save" name="savebutton"/>
    <input type="text" />{{ output }}
    <canvas id="canvas" style="display: none;"><br>
    </canvas>
    <div class="output">
        <img id="photo" alt="The screen capture will appear in this box."><br/>
    </div>
</form>

这是我的 ajax 请求。我正在将我的 canvas 图像数据和选定的单选按钮值发送到 django。

$(document).ready(function() {
                $("#capture").submit(function(event) {
                    var radio_select = document.capture.inlineRadioOptions.value;
                    console.log(radio_select)

                    event.preventDefault();
                    $.ajax({
                        type: "POST",
                        url: 'external',
                        data: {
                            'img64': photo.src,
                            'radio_seleect': radio_select


                        }
                    }).done(function() {
                        console.log('sent');


                    });

                });
            });
     

假设您的 urlpatterns 如下所示: path('external/', views.external, name='ext_data')

if request.is_ajax():
    if out is not None:
        return JsonResponse({'output':out.stdout})

js端

$.ajax({
  ... ...
  url: '/external/',
  ... ...
}).done(function (msg) {
  console.log(msg)
);

检查它是否有效..