最近我将一个 Django 项目移到了一个新的虚拟环境中。一切似乎都工作正常,除了以下错误:
TemplateSyntaxError at /profile/
Invalid filter: 'addition'
Request Method: GET
Request URL: http://example.com/example/
Django Version: 1.9.12
Exception Type: TemplateSyntaxError
Exception Value:
Invalid filter: 'addition'
Exception Location: /opt/example/local/lib/python2.7/site-packages/django/template/base.py in parse, line 516
Python Executable: /usr/local/bin/uwsgi
Python Version: 2.7.3
我将其缩小到这行代码:
{% with deeb_percent=stat.deeb_base|addition:stat.deeb_deal %}
进一步调查,我发现了这一点:https ://github.com/dbrgn/django-mathfilters ,似乎'addition'是一个自定义过滤器,它是mathfilters的一部分。我链接的文档说要运行:
pip install django-mathfilters
我已经检查了 pip freeze,并安装了 mathfilters。
appdirs==1.4.3
backports.ssl-match-hostname==3.5.0.1
beautifulsoup4==4.5.3
Django==1.9.12
django-appconf==1.0.1
django-autocomplete-light==3.2.1
django-compat==1.0.8
django-compressor==1.6
django-dual-authentication==1.0.0
django-hijack==2.0.1
django-htmlmin==0.8.0
django-ipware==1.1.2
django-mathfilters==0.3.0
django-modelcluster==3.0.1
django-taggit==0.22.0
django-treebeard==4.1.0
django-widget-tweaks==1.4.1
djangorestframework==3.6.2
html5lib==0.9999999
packaging==16.8
Pillow==3.0.0
pyparsing==2.2.0
pytz==2015.7
requests==2.13.0
simplejson==3.10.0
six==1.10.0
slackclient==1.0.5
Unidecode==0.4.20
wagtail==1.9
websocket-client==0.40.0
Willow==0.4
然后将 mathfilters 添加到您的 INSTALLED_APPS。
我还检查了项目中的 INSTALLED_APPSsettings.py
并且还加载了 mathfilters:
INSTALLED_APPS = [
...
'mathfilters',
...
]
在有问题的模板顶部加载了“mathfilters”:
{% extends "base.html" %}
{% load i18n %}
{% load static %}
{% load extra %}
{% load mathfilters %}
如果我将模板中的“添加”更改为“添加”,则模板可以正常工作。
{% with deeb_percent=stat.deeb_base|addition:stat.deeb_deal %}
但是我不想为多个地方的每个模板都这样做,并且因为我将项目安装在一个新的 virtualenv 中,所以我也不愿意让这个“损坏”。
为什么数学过滤器不起作用?我该如何解决这个错误?