Skip to content

Instantly share code, notes, and snippets.

@meoooh
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meoooh/f55d394111dce5bf2e3d to your computer and use it in GitHub Desktop.
Save meoooh/f55d394111dce5bf2e3d to your computer and use it in GitHub Desktop.
# reference: https://docs.djangoproject.com/en/1.6/howto/custom-template-tags/
@register.filter
def get_index(l, element):
return l.index(element)
<html>
<head></head>
<body>
<!-- mylist = [0, 1, 3] -->
<p>{{ mylist|get_index:3 }}</p>
</body>
</html>
{% comment "result" %}
result:
<html>
<head></head>
<body>
<!-- mylist = [0, 1, 3] -->
<p>2</p>
</body>
</html>
{% endcomment %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment