diff --git a/django/contrib/staticfiles/templatetags/__init__.py b/django/contrib/staticfiles/templatetags/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/django/contrib/staticfiles/templatetags/staticfiles.py b/django/contrib/staticfiles/templatetags/staticfiles.py
new file mode 100644
index 0000000..5ddff77
-
|
+
|
|
| 1 | from django import template |
| 2 | from django.conf import settings |
| 3 | from django.core.files.storage import get_storage_class |
| 4 | |
| 5 | register = template.Library() |
| 6 | |
| 7 | storage = get_storage_class(settings.STATICFILES_STORAGE)() |
| 8 | |
| 9 | |
| 10 | @register.simple_tag |
| 11 | def staticfiles_url(path): |
| 12 | """ |
| 13 | A template tag that returns the URL to a file |
| 14 | using staticfiles' storage backend |
| 15 | """ |
| 16 | if storage.exists(path): |
| 17 | return storage.url(path) |
| 18 | return '' |
diff --git a/tests/regressiontests/staticfiles_tests/tests.py b/tests/regressiontests/staticfiles_tests/tests.py
index 1e2b549..6ecb01f 100644
a
|
b
|
import sys
|
7 | 7 | import tempfile |
8 | 8 | from StringIO import StringIO |
9 | 9 | |
| 10 | from django.template import loader, Context |
10 | 11 | from django.conf import settings |
11 | 12 | from django.contrib.staticfiles import finders, storage |
12 | 13 | from django.core.exceptions import ImproperlyConfigured |
… |
… |
class StaticFilesTestCase(TestCase):
|
85 | 86 | def assertFileNotFound(self, filepath): |
86 | 87 | self.assertRaises(IOError, self._get_file, filepath) |
87 | 88 | |
| 89 | def assertTemplateRenders(self, template, result, **kwargs): |
| 90 | if isinstance(template, basestring): |
| 91 | template = loader.get_template_from_string(template) |
| 92 | self.assertEqual(template.render(Context(kwargs)), result) |
| 93 | |
88 | 94 | |
89 | 95 | class BuildStaticTestCase(StaticFilesTestCase): |
90 | 96 | """ |
… |
… |
class TestStaticfilesDirsType(TestCase):
|
432 | 438 | |
433 | 439 | def test_non_tuple_raises_exception(self): |
434 | 440 | self.assertRaises(ImproperlyConfigured, finders.FileSystemFinder) |
| 441 | |
| 442 | |
| 443 | class TestTemplateTag(StaticFilesTestCase): |
| 444 | |
| 445 | def test_template_tag(self): |
| 446 | self.assertTemplateRenders("""{% load staticfiles %}{% staticfiles_url "does/not/exist.png" %}""", "") |
| 447 | self.assertTemplateRenders("""{% load staticfiles %}{% staticfiles_url "test/storage.txt" %}""", "/static/test/storage.txt") |