开发者

How to check a List for a match in Django?

Assuming:

strings = ["abc", "a", "bc"]
str = "bc"

I want to:

{% if str is in strings %}

Doesn't look like this is allowed in Django. If开发者_如何学运维 not what is the proper syntax or a method for checking a value in a List?


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)


Programmers don't like extra words. Try:

{% if str in strings %}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜