开发者

How to filter all selected options?

I have these 5 options from pull down menues:

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

I want all selected ones to be filtered. I try this:

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))

But in this case, if image_style is not "None" the other filters will not execute. How do I formulate this so that all selections are ex开发者_JAVA技巧ecuted. Thanks.


Build up a list tags of all the options, filter out all non-selected (see the lambda) and check if the resulting list is empty or not.

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""")
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜