|
1 | 1 | import calendar |
2 | 2 | import datetime |
| 3 | +import os |
| 4 | +import posixpath |
3 | 5 |
|
| 6 | +from django.contrib.staticfiles import finders |
| 7 | +from django.http import Http404 |
4 | 8 | from django.shortcuts import render |
| 9 | +from django.utils.six.moves.urllib.parse import unquote |
| 10 | +from django.views import static |
5 | 11 |
|
6 | 12 | from .models import Event, NewsItem, Page, UserGroup |
7 | 13 |
|
@@ -73,3 +79,29 @@ def news_item(request, year, month, day, slug): |
73 | 79 | 'news_item': news_item, |
74 | 80 | } |
75 | 81 | return render(request, 'ukpython/news_item.html', context) |
| 82 | + |
| 83 | + |
| 84 | +def serve_static(request, path, insecure=False, **kwargs): |
| 85 | + """ |
| 86 | + This is copied from Django's contrib.staticfiles.views to remove the DEBUG |
| 87 | + check. |
| 88 | +
|
| 89 | + We don't need to check DEBUG since we never actually serve the site with |
| 90 | + Django. |
| 91 | +
|
| 92 | + Serve static files below a given point in the directory structure or |
| 93 | + from locations inferred from the staticfiles finders. |
| 94 | + To use, put a URL pattern such as:: |
| 95 | + from django.contrib.staticfiles import views |
| 96 | + url(r'^(?P<path>.*)$', views.serve) |
| 97 | + in your URLconf. |
| 98 | + It uses the django.views.static.serve() view to serve the found files. |
| 99 | + """ |
| 100 | + normalized_path = posixpath.normpath(unquote(path)).lstrip('/') |
| 101 | + absolute_path = finders.find(normalized_path) |
| 102 | + if not absolute_path: |
| 103 | + if path.endswith('/') or path == '': |
| 104 | + raise Http404("Directory indexes are not allowed here.") |
| 105 | + raise Http404("'%s' could not be found" % path) |
| 106 | + document_root, path = os.path.split(absolute_path) |
| 107 | + return static.serve(request, path, document_root=document_root, **kwargs) |
0 commit comments