[Fixed]-How to put an opencv function to play videos in django views?

1👍

Quick answer: Don’t bother with templates and render(), just use an HttpResponse. The video will play, then the response will be returned so it all works out in the end.

from django.http import HttpResponse

def index(request):
    play_video()
    return HttpResponse("OK")

Opinions answer:

So I’ve actually done something kinda similar to this.

I’d recommend having a main view with the button on it, that when clicked calls a JavaScript function that sends a GET request to another hidden view that actually plays the video on the server.

This hidden view would basically be the code snippet I posted above.

You may also want to consider putting your video playing code in a subprocess because Django or the webbrowser might time out or something.

Leave a comment