如何在原型中显示词汇标题?

发布于 2025-01-04 23:40:33 字数 1457 浏览 0 评论 0原文

我希望我的自定义类型显示存储的词汇表的标题。字段定义如下所示:

atapi.LinesField(
    'member_field',
    searchable=1,
    index='KeywordIndex',
    multiValued=1,
    storage=atapi.AnnotationStorage(),
    vocabulary_factory='member_name',
    widget=AutocompleteWidget(
        label=_(u"Member Name"),
        description=_(u"Multiple Lines, One Per Line."),
        actb_timeout=-1,
        actb_expand_onfocus=0,
        actb_filter_bogus=0,
    ),
    enforceVocabulary=0,
),

词汇表定义如下所示:

class member_name(object):
    implements(IVocabularyFactory)
    def __call__(self, context=None):
        items = (
            SimpleTerm(value='john', title=u'John Doe'),
            SimpleTerm(value='paul', title=u'Paul Smith'),
            ... ...
        )
        return SimpleVocabulary(items)
member_nameFactory = member_name()

相应的页面模板如下所示:

<div tal:define="mbrs context/member_field|nothing"
     tal:condition="mbrs">
Member List:
<span tal:repeat="mbr mbrs">
  <span tal:replace="mbr">Member Name</span>
  <span class="separator"
   tal:condition="not: repeat/mbr/end" tal:replace="string:, " />
</span>
</div>

仅显示值的示例结果如下所示: 成员列表: paul , john。我怎样才能显示他们的头衔,例如:成员列表:Paul Smith、John Doe

I want my custom type to display the stored vocabulary's title. The field definition looks like:

atapi.LinesField(
    'member_field',
    searchable=1,
    index='KeywordIndex',
    multiValued=1,
    storage=atapi.AnnotationStorage(),
    vocabulary_factory='member_name',
    widget=AutocompleteWidget(
        label=_(u"Member Name"),
        description=_(u"Multiple Lines, One Per Line."),
        actb_timeout=-1,
        actb_expand_onfocus=0,
        actb_filter_bogus=0,
    ),
    enforceVocabulary=0,
),

The vocabulary definition looks like:

class member_name(object):
    implements(IVocabularyFactory)
    def __call__(self, context=None):
        items = (
            SimpleTerm(value='john', title=u'John Doe'),
            SimpleTerm(value='paul', title=u'Paul Smith'),
            ... ...
        )
        return SimpleVocabulary(items)
member_nameFactory = member_name()

The corresponding page template looks like:

<div tal:define="mbrs context/member_field|nothing"
     tal:condition="mbrs">
Member List:
<span tal:repeat="mbr mbrs">
  <span tal:replace="mbr">Member Name</span>
  <span class="separator"
   tal:condition="not: repeat/mbr/end" tal:replace="string:, " />
</span>
</div>

The example result, showing only values, looks like: Member List: paul , john. How can I display their titles instead, like: Member List: Paul Smith , John Doe?

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

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

发布评论

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

评论(1

避讳 2025-01-11 23:40:33

词汇表(Zope3 风格)只是命名的实用程序,您可以像这样检索它们:

from zope.component import getUtility
from zope.schema.interfaces import IVocabularyFactory

factory = getUtility(IVocabularyFactory, vocabularyname)
vocabulary = factory(self.context)

然后您可以获得术语的标题,如下所示:

fieldvalue = self.context.getField('myfield').get(self.context)
term = vocabulary.getTerm(fieldvalue)

print "Term value is %s token is %s and title is %s" + (term.value, term.token, term.title)

更多信息

Vocabularies (in Zope3 style) are just named utilities and you can retrieve them like this:

from zope.component import getUtility
from zope.schema.interfaces import IVocabularyFactory

factory = getUtility(IVocabularyFactory, vocabularyname)
vocabulary = factory(self.context)

and then you can get the term's title like this:

fieldvalue = self.context.getField('myfield').get(self.context)
term = vocabulary.getTerm(fieldvalue)

print "Term value is %s token is %s and title is %s" + (term.value, term.token, term.title)

More info:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文