Ticket #15252: 15252.1.diff

File 15252.1.diff, 2.3 KB (added by jezdez, 4 years ago)

initial patch

  • new file django/contrib/staticfiles/templatetags/staticfiles.py

    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
    - +  
     1from django import template
     2from django.conf import settings
     3from django.core.files.storage import get_storage_class
     4
     5register = template.Library()
     6
     7storage = get_storage_class(settings.STATICFILES_STORAGE)()
     8
     9
     10@register.simple_tag
     11def 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 ''
  • tests/regressiontests/staticfiles_tests/tests.py

    diff --git a/tests/regressiontests/staticfiles_tests/tests.py b/tests/regressiontests/staticfiles_tests/tests.py
    index 1e2b549..6ecb01f 100644
    a b import sys 
    77import tempfile
    88from StringIO import StringIO
    99
     10from django.template import loader, Context
    1011from django.conf import settings
    1112from django.contrib.staticfiles import finders, storage
    1213from django.core.exceptions import ImproperlyConfigured
    class StaticFilesTestCase(TestCase): 
    8586    def assertFileNotFound(self, filepath):
    8687        self.assertRaises(IOError, self._get_file, filepath)
    8788
     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
    8894
    8995class BuildStaticTestCase(StaticFilesTestCase):
    9096    """
    class TestStaticfilesDirsType(TestCase): 
    432438
    433439    def test_non_tuple_raises_exception(self):
    434440        self.assertRaises(ImproperlyConfigured, finders.FileSystemFinder)
     441
     442
     443class 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")
Back to Top