Django TemplateSyntaxError无效筛选器:';truncatechars';
在我的模板中,当显示数据时,我使用下面的代码截断字符Django TemplateSyntaxError无效筛选器:';truncatechars';,django,django-forms,django-templates,django-views,Django,Django Forms,Django Templates,Django Views,在我的模板中,当显示数据时,我使用下面的代码截断字符 {{list.report.incident_description|default:"No description available"|truncatechars:26}} 但是如果我使用“truncatewords:4”我不会得到错误 它给了我下面的回溯 Template error: In template /root/Projects/ir_uploaded_copy/ir_20-6/ir/templates/incident/i
{{list.report.incident_description|default:"No description available"|truncatechars:26}}
但是如果我使用“truncatewords:4”
我不会得到错误
它给了我下面的回溯
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'
模板错误:
在template/root/Projects/ir_-copy/ir_20-6/ir/templates/incident/index.html中,第22行出现错误
无效筛选器:“truncatechars”
12:安全委员会
13 :
14 :
15 :
16 :
17:文件名
18:事件描述
19:需要采取的行动
20 :
21:{报告中的列表为%u列表%}
22:{%if list.firstaid\u启用或list.notification\u启用或list.followup\u启用或list.safty\u notes\u启用%}
23 :
24 :
25:{{list.report.incident|description |默认值:“无可用描述”| truncatechars:26}
26 :
27:{%if list.first-aid_enabled%}
28:{%endif%}
31 :
32 :
回溯:
get_响应中的文件“/usr/lib/python2.7/site packages/django/core/handlers/base.py”
111响应=回调(请求,*回调参数,**回调参数)
视图中的文件“/usr/lib/python2.7/site packages/django/contrib/auth/decorators.py”
23返回视图功能(请求,*args,**kwargs)
文件“/root/Projects/ir\u上传\u copy/ir\u 20-6/ir/incident/views.py”在索引中
85. '报告列表:报告列表
render中的文件“/usr/lib/python2.7/site packages/django/shortcuts/\uuuuuu init\uuuuuu.py”
44返回HttpResponse(loader.render_to_string(*args,**kwargs),
render_to_字符串中的文件“/usr/lib/python2.7/site packages/django/template/loader.py”
181.t=获取模板(模板名称)
get_模板中的文件“/usr/lib/python2.7/site packages/django/template/loader.py”
157.模板,原点=查找模板(模板名称)
查找模板中的文件“/usr/lib/python2.7/site packages/django/template/loader.py”
134.源,显示\名称=加载器(名称,目录)
调用中的文件“/usr/lib/python2.7/site packages/django/template/loader.py”__
42.返回自加载模板(模板名称、模板目录)
load_模板中的文件“/usr/lib/python2.7/site packages/django/template/loader.py”
48.template=从字符串(源、源、模板名称)获取模板
文件“/usr/lib/python2.7/site packages/django/template/loader.py”,位于get\u template\u from\u字符串中
168.返回模板(来源、来源、名称)
init中的文件“/usr/lib/python2.7/site packages/django/template/base.py”__
108.self.nodelist=编译字符串(模板字符串,原点)
编译字符串中的文件“/usr/lib/python2.7/site packages/django/template/base.py”
136.返回parser.parse()
解析中的文件“/usr/lib/python2.7/site packages/django/template/base.py”
239.编译结果=编译函数(self,token)
do_扩展中的文件“/usr/lib/python2.7/site packages/django/template/loader_tags.py”
214.nodelist=parser.parse()
解析中的文件“/usr/lib/python2.7/site packages/django/template/base.py”
239.编译结果=编译函数(self,token)
do_块中的文件“/usr/lib/python2.7/site packages/django/template/loader_tags.py”
192.nodelist=parser.parse(('endblock','endblock%s'%block\u name))
解析中的文件“/usr/lib/python2.7/site packages/django/template/base.py”
239.编译结果=编译函数(self,token)
dou中的文件“/usr/lib/python2.7/site packages/django/template/defaulttags.py”,用于
nodelist_loop=parser.parse(('empty','endfor',))
解析中的文件“/usr/lib/python2.7/site packages/django/template/base.py”
239.编译结果=编译函数(self,token)
do_if中的文件“/usr/lib/python2.7/site packages/django/template/defaulttags.py”
nodelist_true=parser.parse(('else','endif'))
解析中的文件“/usr/lib/python2.7/site packages/django/template/base.py”
220.filter\u expression=self.compile\u过滤器(token.contents)
编译过滤器中的文件“/usr/lib/python2.7/site packages/django/template/base.py”
314.返回筛选器表达式(令牌、自身)
init中的文件“/usr/lib/python2.7/site packages/django/template/base.py”__
497.filter\u func=parser.find\u filter(filter\u name)
查找过滤器中的文件“/usr/lib/python2.7/site packages/django/template/base.py”
320.raise TemplateSyntaxError(“无效筛选器:“%s]”%filter\u name)
异常类型:TemplateSyntaxError位于/member/
异常值:无效筛选器:“truncatechars”
已添加Truncatechars。看起来您使用的是django 1.4版之前的版本
您可以使用来实现django-1.4之前的功能
{{ list.report.incident_description|default:"No description available"|slice:"26" }}{% if list.report.incident_description|length > 26 %}...{% endif %}
确切地说,我使用的是Django 1.3.7,所以我应该如何处理它。这3个点的长度也不到26。如果描述是“这是测试”,那么输出是这样的“这是测试…”-->这不应该发生什么?真的吗?
{list.report.incident|description}
给你什么?