Ticket #4105: simple_media_tag.patch

File simple_media_tag.patch, 2.2 KB (added by SmileyChris, 8 years ago)

Simpler take

  • django/template/defaulttags.py

     
    771771    return LoadNode()
    772772load = register.tag(load)
    773773
     774#@register.simple_tag
     775def media(path=''):
     776    """
     777    Returns an absolute URL pointing to the given media URL.
     778
     779    The optional first argument is the path to the file starting from
     780    ``settings.MEDIA_URL``.
     781
     782    For example, if your ``MEDIA_URL`` is ``'http://media.example.com'``
     783    then in your template you can get the URL for ``css/mystyle.css``
     784    like this:
     785
     786        {% media 'css/mystyle.css' %}
     787
     788    The URL returned would be ``http://media.example.com/css/style.css``.
     789
     790    If no argument is given, the tag will just output the ``MEDIA_URL``
     791    (appending a trailing slash if it does not end in one). For example,
     792    ``{% media %}`` would return ``http://media.example.com/``.
     793    """
     794    import urlparse
     795    url = settings.MEDIA_URL or ''
     796    if path:
     797        return urlparse.join(url, path)
     798    elif not url.endswith('/'):
     799        return '%s/' % url
     800    return url
     801media = register.simple_tag(media)
     802
    774803#@register.tag
    775804def now(parser, token):
    776805    """
  • docs/templates.txt

     
    625625
    626626See `Custom tag and filter libraries`_ for more information.
    627627
     628media
     629~~~~~
     630
     631Return an absolute URL pointing to the given media URL.
     632
     633The optional first argument is the path to the file starting from `MEDIA_URL`_
     634in your Django settings.
     635
     636.. _MEDIA_URL: ../settings/#media-url
     637
     638For example, if your ``MEDIA_URL`` is ``'http://media.example.com'`` then in
     639your template you can get the URL for ``css/mystyle.css`` like this::
     640
     641    {% media 'css/mystyle.css' %}
     642
     643The URL returned would be ``http://media.example.com/css/style.css``.
     644
     645If no argument is given, the tag will just output the ``MEDIA_URL`` (appending
     646a trailing slash if it does not end in one). For example, ``{% media %}`` would
     647return ``http://media.example.com/``.
     648
    628649now
    629650~~~
    630651
Back to Top