如何使 filter() 的参数成为变量?
我有这个模型
class Item(db.Model):
...
glam = db.StringProperty()
casual = db.StringProperty()
speaking = db.StringProperty()
和这个带有单选按钮表单的处理程序:
class SortForm(webapp.RequestHandler):
def get(self):
self.response.out.write("""
<form name="submit_form" action="/sortformhandler" method="post">
Category: <input type="radio" name="image" value="image"> Image <br />
Sorty by tag: <br />
<input type="radio" name="tag" value="glam" /> Glam <br />
<input type="radio" name="tag" value="formal" /> Formal <br />
<input type="radio" name="tag" value="speaking" /> Speaking <br />
<input type="submit" value="submit">
</form>
""")
并且这个处理程序
class SortFormHandler(webapp.RequestHandler):
def post(self):
query = Item.all()
query.filter("glam =", "glam")
for item in query:
self.response.out.write("""<a href="%s"><image src="%s" height="110">%s</a>""" %
(item.url, item.image_source_url, item.title) )
我一直在尝试拥有类似的东西
query.filter("[self.request.get("tag")] =", [self.request.get("tag")])
,以便当在单选按钮中选择 glam
时我应该有
query.filter("glam =", "glam")
但我不能使这项工作发挥作用。换句话说,我试图将 filter()
的参数设为变量。有什么建议吗?
我正在尝试为图像库创建标签。谢谢。
I have this model
class Item(db.Model):
...
glam = db.StringProperty()
casual = db.StringProperty()
speaking = db.StringProperty()
and this handler with a form with radio buttons:
class SortForm(webapp.RequestHandler):
def get(self):
self.response.out.write("""
<form name="submit_form" action="/sortformhandler" method="post">
Category: <input type="radio" name="image" value="image"> Image <br />
Sorty by tag: <br />
<input type="radio" name="tag" value="glam" /> Glam <br />
<input type="radio" name="tag" value="formal" /> Formal <br />
<input type="radio" name="tag" value="speaking" /> Speaking <br />
<input type="submit" value="submit">
</form>
""")
and this handler
class SortFormHandler(webapp.RequestHandler):
def post(self):
query = Item.all()
query.filter("glam =", "glam")
for item in query:
self.response.out.write("""<a href="%s"><image src="%s" height="110">%s</a>""" %
(item.url, item.image_source_url, item.title) )
I've been trying to have something like
query.filter("[self.request.get("tag")] =", [self.request.get("tag")])
so that when glam
is chosen in the radio button I should have
query.filter("glam =", "glam")
But I could not make this work. In other words, I am trying to make the argument to filter()
a variable. Any suggestions?
I am trying to create tagging for the image library. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这会满足您的需求吗:
但是,我同意下面的 Wooble 观点。按照您的设计方式,您实际上并没有使用
glam
、casual
、speaking
作为StringProperty
,因为它们要么为空,要么具有特定值。您可能想要做的是拥有一个
tag
属性,该属性可以采用来自 glam、formal、speaking 的不同值,...然后你会像这样查询你的数据库:
Would that do what you're looking for:
However, I agree with Wooble below. The way you have designed it, you dont' really use
glam
,casual
,speaking
asStringProperty
, since they are either empty or have a specific value.What you probably want to do is have a
tag
property that can take different values from glam, formal, speaking, ...And then you would query your db like so:
=
实际上不是必需的。不过,我还会考虑为标记使用单个 StringProperty,因为看起来您的 3 个字符串属性本质上是布尔值,并且一次只能其中一个为 true。
The
=
is actually not required.However, I'd also consider using a single StringProperty for the tag, since it appears that your 3 string properties are essentially booleans, and only one of them can be true at a time.