如何用字符串格式表示 ListProperty(str) 属性?

发布于 2024-10-14 19:07:28 字数 2756 浏览 0 评论 0原文

我有一个 ListProperty(str)

class Item(db.Model):
    tags = db.ListProperty(str)

有时我需要编辑已写入数据存储的标签,因此我创建了一个编辑页面:

class EditItem(webapp.RequestHandler):
    def get(self):
        self.response.out.write("""<div>
        <form name="submit_form" action="/edititemhandler" method="post">

        url: <input type="text" name="url" size=50><br />
        <input type="submit" value="submit">
        </form></div>""")

这处理了表单:

class EditItemHandler(webapp.RequestHandler):
    def post(self):
            query = Item.all()
            query.filter("url =", self.request.get("url"))
            e = query.get()
            self.response.out.write("""
            <form name="submit_form" action="/edititemhandlerhandler" method="post">
            tags: <input type="text" name="tags" size=50 value="%s"> <br />
            [other values]
            ...
            <input type="submit" value="submit">
            </form> """ % (e.tags, .... ))

编辑标签后,我尝试编写 new_tags 返回数据库:

class EditItemHandlerHandler(webapp.RequestHandler):
    def post(self):
        query = Item.all()
        query.filter("url =", self.request.get("url"))
        item = query.get()
        logging.info("TAGS %s" % self.request.get("tags"))
        new_tags = self.request.get("tags")
        item.tags = new_tags
        [other items]
        ...
        item.put()

但这给出了

BadValueError('Property %s must be a list' % self.name)
BadValueError: Property tags must be a list

But logging.info 显示 self.request.get("tags") 看起来像一个列表:

TAGS [u'speaking', u'glam']

如果我确实

    item.tags = [new_tags]

new_tags 转换为我得到的列表,

"[u'presidential', u'glam', u'family']" 

它看起来不像列表,但看起来像字符串。

那么,如何使用 %s 格式化列表元素以使其保持列表状态?谢谢。

更新

我想我需要在更新之前将标签列表转换为字符串,然后将其转换为列表:

>>> tag_list = [u'formal', u'flag', u'photo']
>>> tag_list_as_string = str(" ".join(tag_list))
>>> tag_list_as_string
'formal flag photo'
>>> #edit tags
>>> tag_list_as_string = 'formal flag photo newtag'
>>> new_tag_list = tag_list_as_string.split(" ")
>>> new_tag_list
['formal', 'flag', 'photo', 'newtag']
>>>

你知道我如何获得带有逗号的tag_list_as_string,就像这样,

'formal, flag, photo'

而不是像这样

'formal flag photo' 

谢谢

I have a ListProperty(str):

class Item(db.Model):
    tags = db.ListProperty(str)

Sometimes I need to edit the tags already written to datastore, so I created an edit page:

class EditItem(webapp.RequestHandler):
    def get(self):
        self.response.out.write("""<div>
        <form name="submit_form" action="/edititemhandler" method="post">

        url: <input type="text" name="url" size=50><br />
        <input type="submit" value="submit">
        </form></div>""")

This handles the form:

class EditItemHandler(webapp.RequestHandler):
    def post(self):
            query = Item.all()
            query.filter("url =", self.request.get("url"))
            e = query.get()
            self.response.out.write("""
            <form name="submit_form" action="/edititemhandlerhandler" method="post">
            tags: <input type="text" name="tags" size=50 value="%s"> <br />
            [other values]
            ...
            <input type="submit" value="submit">
            </form> """ % (e.tags, .... ))

After I edit the tags I try to write new_tags back to the database:

class EditItemHandlerHandler(webapp.RequestHandler):
    def post(self):
        query = Item.all()
        query.filter("url =", self.request.get("url"))
        item = query.get()
        logging.info("TAGS %s" % self.request.get("tags"))
        new_tags = self.request.get("tags")
        item.tags = new_tags
        [other items]
        ...
        item.put()

But this gives

BadValueError('Property %s must be a list' % self.name)
BadValueError: Property tags must be a list

But logging.info shows self.request.get("tags") to look like a list:

TAGS [u'speaking', u'glam']

If I do

    item.tags = [new_tags]

to turn new_tags into list I get

"[u'presidential', u'glam', u'family']" 

which does not look like a list but looks like a string.

So, how do I format the list element with %s so that it stays a list? Thanks.

Update

I figured that I needed to convert tag list to string before updating and then convert it into list:

>>> tag_list = [u'formal', u'flag', u'photo']
>>> tag_list_as_string = str(" ".join(tag_list))
>>> tag_list_as_string
'formal flag photo'
>>> #edit tags
>>> tag_list_as_string = 'formal flag photo newtag'
>>> new_tag_list = tag_list_as_string.split(" ")
>>> new_tag_list
['formal', 'flag', 'photo', 'newtag']
>>>

Do you know how I can get the tag_list_as_string with commas, like this,

'formal, flag, photo'

instead of like this

'formal flag photo' 

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长途伴 2024-10-21 19:07:28
tag_list = [u'formal', u'flag', u'photo']
print ", ".join(tag_list)
>> formal, flag, photo
tag_list = [u'formal', u'flag', u'photo']
print ", ".join(tag_list)
>> formal, flag, photo
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文