Skip to content

Instantly share code, notes, and snippets.

@rixx
Created June 18, 2018 22:15
Show Gist options
  • Save rixx/b8d6c73da48b280a65e144918f7f96b7 to your computer and use it in GitHub Desktop.
Save rixx/b8d6c73da48b280a65e144918f7f96b7 to your computer and use it in GitHub Desktop.
Delivering static files on fixed URL in Django
import os
from django.http import Http404, FileResponse
def get_static(request, path, content_type):
path = os.path.join(settings.BASE_DIR, 'pretalx/static', path)
if not os.path.exists(path):
raise Http404()
return FileResponse(open(path, 'rb'), content_type=content_type)
urlpatterns = [
url('^sw.js', get_static, {'path': 'agenda/js/serviceworker.js', 'content_type': 'application/javascript'})
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment