2

在我的模板中显示数据时,我使用下面的代码截断了字符。

{{list.report.incident_description|default:"No description available"|truncatechars:26}}

但是如果我使用"truncatewords:4"我没有收到错误

它给了我下面的 Traceback。

Template error:
In template /root/Projects/ir_uploaded_copy/ir_20-6/ir/templates/incident/index.html, error at line 22
   Invalid filter: 'truncatechars'
   12 :         <p>Safety Committee</p><img src="{{ STATIC_URL }}images/safety_committee.png" width="20" height="21" alt="" />
   13 :     </span>        
   14 :     <div id="report-list">        
   15 :         <table class="lines" width="100%">        
   16 :             <tr>        
   17 :                 <th>Filename</th>        
   18 :                 <th>Incident Description</th>        
   19 :                 <th id=action-needed colspan="4">Action needed</th>        
   20 :             </tr>        
   21 :             {% for list in report_list%}        
   22 :              {% if  list.firstaid_enabled or list.notification_enabled or list.followup_enabled or list.safty_notes_enabled %}         
   23 :             <tr>        
   24 :                 <td class="irlist-num"><a href="{% url incident.views.edit_report list.report.id%}">{{list.report.incident_number}}{%if list.report.is_draft%} DRAFT{% endif %}</a> </td>        
   25 :                 <td class="irlist-desc">{{list.report.incident_description|default:"No description available"|truncatechars:26}} </td>        
   26 :                 <td class="actions">        
   27 :                       {%if list.firstaid_enabled%}         
   28 :                       <a href="{% url incident.views.edit_report list.report.id%}">        
   29 :                        <img src="{{ STATIC_URL }}images/{% if list.first_aid_needed%}firstaid.JPG{% else %}firstaid_tick.JPG {%endif%}" width="20" height="21" alt="" />    
   30 :                      </a> {%endif%}    
   31 :                 </td>        
   32 :                 <td class="actions">    
Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)
File "/root/Projects/ir_uploaded_copy/ir_20-6/ir/incident/views.py" in index
  85.                   'report_list': report_list
File "/usr/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render
  44.     return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/usr/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  181.         t = get_template(template_name)
File "/usr/lib/python2.7/site-packages/django/template/loader.py" in get_template
  157.     template, origin = find_template(template_name)
File "/usr/lib/python2.7/site-packages/django/template/loader.py" in find_template
  134.             source, display_name = loader(name, dirs)
File "/usr/lib/python2.7/site-packages/django/template/loader.py" in __call__
  42.         return self.load_template(template_name, template_dirs)
File "/usr/lib/python2.7/site-packages/django/template/loader.py" in load_template
  48.             template = get_template_from_string(source, origin, template_name)
File "/usr/lib/python2.7/site-packages/django/template/loader.py" in get_template_from_string
  168.     return Template(source, origin, name)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in __init__
  108.         self.nodelist = compile_string(template_string, origin)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in compile_string
  136.     return parser.parse()
File "/usr/lib/python2.7/site-packages/django/template/base.py" in parse
  239.                     compiled_result = compile_func(self, token)
File "/usr/lib/python2.7/site-packages/django/template/loader_tags.py" in do_extends
  214.     nodelist = parser.parse()
File "/usr/lib/python2.7/site-packages/django/template/base.py" in parse
  239.                     compiled_result = compile_func(self, token)
File "/usr/lib/python2.7/site-packages/django/template/loader_tags.py" in do_block
  192.     nodelist = parser.parse(('endblock', 'endblock %s' % block_name))
File "/usr/lib/python2.7/site-packages/django/template/base.py" in parse
  239.                     compiled_result = compile_func(self, token)
File "/usr/lib/python2.7/site-packages/django/template/defaulttags.py" in do_for
  787.     nodelist_loop = parser.parse(('empty', 'endfor',))
File "/usr/lib/python2.7/site-packages/django/template/base.py" in parse
  239.                     compiled_result = compile_func(self, token)
File "/usr/lib/python2.7/site-packages/django/template/defaulttags.py" in do_if
  922.     nodelist_true = parser.parse(('else', 'endif'))
File "/usr/lib/python2.7/site-packages/django/template/base.py" in parse
  220.                 filter_expression = self.compile_filter(token.contents)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in compile_filter
  314.         return FilterExpression(token, self)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in __init__
  497.                 filter_func = parser.find_filter(filter_name)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in find_filter
  320.             raise TemplateSyntaxError("Invalid filter: '%s'" % filter_name)

Exception Type: TemplateSyntaxError at /member/
Exception Value: Invalid filter: 'truncatechars'
4

1 回答 1

2

Truncatechars 是在django 1.4中添加的。看起来您使用的是 django 1.4 版之前的版本

你可以使用slicedjango-1.4 来实现

{{ list.report.incident_description|default:"No description available"|slice:"26" }}{% if list.report.incident_description|length > 26 %}...{% endif %}
于 2013-06-20T16:30:20.580 回答