djangodjango-templatesdjango-media

Video not playing on Django when debug=False


Videos are not playing when I turn DEBUG to False on Django, but on DEBUG=True, everything is fine. The task is simple, the app receives youtube shorts link, downloads it to the MEDIA_ROOT (which declared in settings.py as BASE_DIR / 'media'). And it returns rendered template video.html, on template:
<video height="450" autoplay="autoplay" controls style=""> <br> <source src="{{ MEDIA_URL }}{{ file }}" type="{{ mime_type }}">
on html :
<video height="450" autoplay="autoplay" controls style="">
<source src="/media/filename.mp4" type="video/mp4">

views: {"MEDIA_URL": MEDIA_URL, "file": str(video.video).split('/')[-1],}

settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = Path(__file__).resolve().parent.parent / 'media'

As I said when debug mode turned on everything works perfect, but I turned it off video is not playing. Did I forget something? Or any mistake I have? how to solve this problem?

P.S. I am pretty new on django, I searched for many sources, tried them all, but I couldn't solve this problem


Solution

  • Django does not support file serving in production (have a look here). The helper function "+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) " works only in debug mode. Therefore it cannot read the file from media.

    If you want to use the file in production with debug=False, try to switch to another storage backend or serve it from elsewhere.