[Django]-How to check a List for a match in Django?

4👍

This is an app running on Google App Engine. Here is a custom filter that will do the trick:

from google.appengine.ext.webapp import template
from django import template as django_template

def in_list(value, arg):
  """
  Given an item and a list, check if the item is in the list.
  Usage:
  {% if item|in_list:list %} 
      in list 
  {% else %} 
      not in list
  {% endif %}
  """
  return value in arg

register = template.create_template_register()  
ifinlist = register.filter(in_list)

1👍

Programmers don’t like extra words. Try:

{% if str in strings %}

Leave a comment