处理从python请求库收到的数据,以在模板中显示它们

时间:2019-05-23 14:40:14

标签: django python-3.x django-rest-framework django-templates python-requests

我正在尝试从Raspberry pi获取实时数据并将其显示到我的django模板中。我可以通过“请求”库发送数据,并通过Django REST接收数据。但是问题出在我的django中,例如,我将数据发布到localhost / a,例如,如何将这些数据渲染到localhost / b。

我尝试了django会话。我以为,当我在localhost / b中时,我可以使用会话,但是无法正常工作。

rpi-req.py

# this is code from Raspberry pi, to send(POST) data to django

while True:
    # print(cam.get_frame())
    da = {'rpiLive': "30 or any data"}
    res = requests.post(url, files=da, timeout=1000, auth=('anas', '****'))
    print(res.status_code)
    time.sleep(1)

django中的

views.py。

# this code is from django-views, to receive data
@api_view(['POST'])
def video_feed(request):
    if request.method == 'POST':
        reqData = request.FILES['rpiLive']
        try:
            return HttpResponse(gen22(request, reqData),
                                content_type='multipart/x-mixed-replace; boundary=reqData')
        except Exception as q:
            print("live Data aborted!!!", q)

        return HttpResponseRedirect('/live2/')
    return HttpResponseRedirect('/')

gen22函数是:

def gen22(request, data):
    a = data
    time.sleep(0.5)
    con = {
        "anas": a,
        "con_tag": "live",
    }
    time.sleep(1)
    return render(request, "dahsboard.html", con)

urls.py

path('<int:user_id>/<tag>/', views.dahsboard, name='dahsboard'),
path('live/', views.video_feed, name='live-frame2'),

0 个答案:

没有答案