如何过滤所有选定的选项?

时间:2011-02-04 15:26:28

标签: python google-app-engine conditional

我从下拉菜单中得到以下5个选项:

<select name="image_style">
<select name="image_background">
<select name="image_activity">
<select name="image_merchandise">
<select name="image_type">

我想要过滤所有选定的。我试试这个:

image_background = self.request.get("image_background")
image_activity = self.request.get("image_activity")
image_merchandise = self.request.get("image_merchandise")
image_type = self.request.get("image_type")

items = Image.all()

if image_style != "None":
    items.filter("image_tags =", image_style)
elif image_background != "None":
    items.filter("image_tags =", image_background)
elif image_activity != "None":
    items.filter("image_tags =", image_activity)
elif image_merchandise != "None":
    items.filter("image_tags =", image_merchandise)
elif image_type != "None":
    items.filter("image_tags =", image_type)
else:
    self.response.out.write(""" select tags to sort images""")

for item in items:                               
    self.response.out.write("""
    <a href="%s"><image src="%s" height="110"> </a>  """ % 
    (item.image_url, item.image_source_url))

但在这种情况下,如果image_style不是"None",则其他过滤器将不会执行。如何制定此选项以便执行所有选择。谢谢。

1 个答案:

答案 0 :(得分:3)

构建所有选项的列表tags,过滤掉所有未选中的列表(请参阅lambda)并检查结果列表是否为空。

selects = filter(lambda x: x != "None", 
              [image_style, image_background, image_activity,
               image_merchandise, image_type]
          )

if selects:
    for choice in selects:
        items.filter("image_tags =", choice)
else:
    self.response.out.write(""" select tags to sort images""")