If you're in the situation that you have an authenticated Django view that returns
files for download, you may have noticed that Safari 9.x doesn't play audio files
properly when returning audio-files. The reason is that Safari sends a HTTP_RANGE request header and expects a proper Content-Range response header in return.
There is a Django ticket
for this, however no indication that this feature will be implemented soon.
Choose proper one by your situation.
The WhiteNoise provides a middleware to support global settings.
Nginx can support static and media files.
See more details on Setting up Django and your web server with uWSGI and nginx. If some views need redirect to static or media files, see the stackoverflow answer.
This project supports RangedFileResponse with content range. Use RangedFileResponse for every view you wanted. The views are mostly designed for media API, like speech synthesis, video generators.
pip install django-ranged-response
You can use it for custom views like:
import io
from ranged_response import RangedFileResponse
def some_proxy_view(request):
filename = 'myfile.wav'
response = RangedFileResponse(request, open(filename, 'rb'), content_type='audio/wav')
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
return response
def some_dynamic_view(request):
binarydata = b'RIFF...'
response = RangedFileResponse(request, io.BytesIO(binarydata), content_type='audio/wav')
response['Content-Disposition'] = 'attachment; filename="myfile.wav"'
return response
The original suggested fix applies the code to Django's static view. This is a packaged version of that fix, but uses a modified FileResponse, instead of applying it to Django's static view.