Skip to content

Instantly share code, notes, and snippets.

@SmileyChris
Forked from rixx/urls.py
Last active June 18, 2018 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SmileyChris/8d472f2a67526e36f39f3c33520182bc to your computer and use it in GitHub Desktop.
Save SmileyChris/8d472f2a67526e36f39f3c33520182bc to your computer and use it in GitHub Desktop.
Delivering static files on fixed URL in Django
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