from django import template register = template.Library() @register.filter def dom_id(el, prefix=None): """ {{book|dom_id}} # book_1 {{book|dom_id:'prefix'}} # prefix_book_1 """ return '_'.join([part for part in [prefix, el._meta.module_name, str(el.id)] if part])