如何解决django-截断模板标签的输出?
| 我有一个输出文本的模板标签。如何通过truncate_words运行它? 例如我的搜索结果通过以下方式返回:{% get_translated_value result \"text\" %}
如何通过truncate_words:50运行它?
答案是(我几个小时不能更新此帖子):
{% filter truncatewords:30 %}
{% get_translated_value result \"text\" %}
{% endfilter %}
解决方法
from django import template
from django.utils.encoding import force_unicode
register = template.Library()
def truncate(value,arg = None):
if arg:
return force_unicode(value)[:arg]
return value
register.filter(\'truncate\',truncate)
还有一些其他选择(没有尝试过...自己动手了):
http://djangosnippets.org/snippets/163/
http://djangosnippets.org/snippets/194/
http://djangosnippets.org/snippets/1471/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。