Skip to main content
 首页 » 编程设计

python之音频不播放。 Django,DEBUG = False

2025年05月04日42Terrylee

再次播放音频时出现问题。现在使用 DEBUG = False 进行部署。 这是我的设置:

    STATIC_ROOT = '/home/bootuz/final/myapp/static/' 
 
    STATIC_URL = '/static/' 
 
    MEDIA_ROOT = '/home/bootuz/final/audio/' 
 
    MEDIA_URL = '/media/' 

这段代码我添加到 urls.py

+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

模板:

<audio id="player" preload="auto"> 
                <source src="{{ showword.audio.url }}" type="audio/mpeg"> 
                </audio> 
                <button id="player_button" onclick="document.getElementById('player').play()"><img src="{% static 'images/audio.png' %}" alt="Play"> 
                </button> 

在控制台中我看到这个错误:

伙计们,你能告诉我这是怎么回事吗?

请您参考如下方法:

正如其他人在评论中所说,使用 Django 提供静态文件是不可取的。事实上,the documentation关于 static() 函数有这样的说法:

This is not suitable for production use! For some common deployment strategies, see Deploying static files.

并进一步指出:

This helper function works only in debug mode and only if the given prefix is local (e.g. /static/) and not a URL (e.g. http://static.example.com/).

Also this helper function only serves the actual STATIC_ROOT folder; it doesn’t perform static files discovery like django.contrib.staticfiles.

请引用linked documentation对于在生产中提供静态文件的常见场景。