本文整理汇总了Python中django.template.defaultfilters.truncatewords方法的典型用法代码示例。如果您正苦于以下问题:Python defaultfilters.truncatewords方法的具体用法?Python defaultfilters.truncatewords怎么用?Python defaultfilters.truncatewords使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.template.defaultfilters
的用法示例。
在下文中一共展示了defaultfilters.truncatewords方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_opengraph
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def generate_opengraph(cache_key, data, style):
metadata = cache.get(cache_key)
if metadata is None:
description = None
tree = reference(markdown(data, style)).tree
for p in tree.iterfind('.//p'):
text = p.text_content().strip()
if text:
description = text
break
if description:
for remove in (r'\[', r'\]', r'\(', r'\)'):
description = description.replace(remove, '')
img = tree.xpath('.//img')
metadata = truncatewords(description, 60), img[0].get('src') if img else None
cache.set(cache_key, metadata, 86400)
return metadata
示例2: item_description
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def item_description(self, item):
return truncatewords(item.body, 30)
示例3: truncated_title
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def truncated_title(self):
"""
This PR's title truncated to 4 words
"""
return truncatewords(self.title, 4)
示例4: get_change_message
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def get_change_message(self, obj):
return truncatewords(obj.get_change_message(), 23)
示例5: get_truncated_description
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def get_truncated_description(self, obj):
return defaultfilters.truncatewords(obj.description, 25)
示例6: get_truncated_event_name
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def get_truncated_event_name(self, obj):
return defaultfilters.truncatewords(obj.event.name, 8)
示例7: get_truncated_sponsors
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def get_truncated_sponsors(self, obj):
spons = ", ".join(s.name for s in obj.sponsorships.all()[:5])
return defaultfilters.truncatewords(spons, 10)
示例8: get_truncated_title
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def get_truncated_title(self, obj):
return defaultfilters.truncatewords(obj.title, 25)
示例9: search_terms_
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def search_terms_(self, instance: SearchQuery) -> str:
"""Return truncated version of search_terms."""
raw = instance.search_terms
# take first five words, and further truncate to 50 chars if necessary
return truncatechars(truncatewords(raw, 5), 50)
示例10: item_description
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def item_description(self, item):
return truncatewords(item.content, 30)
示例11: short_detail
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def short_detail(self):
return truncatewords(self.detail, 15)
示例12: test_truncate
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def test_truncate(self):
self.assertEqual(truncatewords('A sentence with a few words in it', 1), 'A ...')
示例13: test_truncate2
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def test_truncate2(self):
self.assertEqual(
truncatewords('A sentence with a few words in it', 5),
'A sentence with a few ...',
)
示例14: test_overtruncate
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def test_overtruncate(self):
self.assertEqual(
truncatewords('A sentence with a few words in it', 100),
'A sentence with a few words in it',
)
示例15: test_invalid_number
# 需要导入模块: from django.template import defaultfilters [as 别名]
# 或者: from django.template.defaultfilters import truncatewords [as 别名]
def test_invalid_number(self):
self.assertEqual(
truncatewords('A sentence with a few words in it', 'not a number'),
'A sentence with a few words in it',
)