A simple django basename template filter tag

Last updated on Thu, 2009-04-16 13:52. Originally submitted by fabio on 2008-08-29 12:42.

I'm using Django, a python coded framework to develop web applications, to implement a project for the University.

During the development I needed to have a template tag which, given a path to a file, just returns its basename (just as it do the python os.path.basename).

So I implemented it. Following you have the code for it.

from django import template
from django.template.defaultfilters import stringfilter
import os


register = template.Library()


@register.filter(name='basename')
@stringfilter
def basename(value):
    return os.path.basename(value)

In order to use this tag just:

  1. copy the above code into a file called mytags.py under a directory called templatetags just in the same level of your manage.py file in your application root.
  2. create an empty __init__.py file in the templatetags directory (so that python knows this is a package)
  3. in your template just after the {% extends .. %} tags insert the {% load mytags %} tag.
  4. use your new filter to get filenames from a full file path:
    {{ render.media_file|basename }}

Note: The above filter will work also on URLs but only on *nix system as os.path.basename will expect a \ under windows. The filter code should be modified and made more complex to support URLs under windows.. but this is out of my needs now. Post a comment if you implement it or if you just have ideas.

For more informations read Django's documentation on writing custom templates tags and filters.

Django - Embed videos on unbuntu host

Submitted by Robert (not verified) on Tue, 2009-09-29 23:34.

I visited this page to learn how to use django to create the magic needed to show embedded videos,, eg as at [http://excess.org/article/2009/06/django-1-1-talk-video/], but I am probably missing some basic reference to how to do this, with examples.

Actually, once you understand

Submitted by fabio on Wed, 2009-09-30 09:02.

Actually, once you understand how django works, writing some templates to embed videos wont be hard. It's just a matter of putting everything together.

You might wanna have a look at djvideo, a django templatetag library to embed video.

Hope this helps, ask for more help if you need it.

Serious typo in your code

Submitted by Anonymous (not verified) on Thu, 2009-04-16 10:53.

Hi your code saved me some time. At the same time it also cost me half an hour trying to debug why it kept saying

'mytags' is not a valid tag library: Could not load template library from django.templatetags.mytags, No module named so.

The problem is that there is a typo in your source code. It should say

import os

instead of

import so

I'm sorry for the error.

Submitted by fabio on Thu, 2009-04-16 13:53.

I'm sorry for the error. Fixed that. thanks for reporting it.

Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a personal or company website insert its address in the form http://www.example.com/ .
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <del> <img> <h2> <h3> <h4> <b> <video> <sub> <sup>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • You may insert videos with [video:URL]
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

More information about formatting options