-
-
Save SmileyChris/8d472f2a67526e36f39f3c33520182bc to your computer and use it in GitHub Desktop.
Delivering static files on fixed URL in Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import mimetypes | |
from django.contrib.staticfiles.storage import staticfiles_storage | |
from django.http import Http404, FileResponse | |
def static_file(request, path, content_type=None): | |
if not staticfiles_storage.exists(path): | |
raise Http404() | |
content_type = content_type or mimetypes.guess_type(path)[0] | |
response = FileResponse(content_type=content_type) | |
with staticfiles_storage.open(path) as f: | |
response.write(f) | |
return response | |
urlpatterns = [ | |
url(r'^sw.js$', static_file, {'path': 'agenda/js/serviceworker.js'}) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment