Skip to content

Instantly share code, notes, and snippets.

@chroto
Created July 13, 2012 21:21
Show Gist options
  • Save chroto/3107564 to your computer and use it in GitHub Desktop.
Save chroto/3107564 to your computer and use it in GitHub Desktop.
Django - detect MIME type
{% for file in mixed_files %}
<a href=""
{% if "image" in file|mimetype %}class="fancybox"{% endif %}
{% if "pdf" in file|mimetype %}rel="external"{% endif %}
>foo</a>
{% endfor %}
from mimetypes import guess_type
@register.filter
def mimetype(f):
"""
Returns a string of the mime type for a file object
"""
return guess_type(f.name)[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment