如何解决“'mathfilter' 标记 'addition' 自复制 django 项目后停止工作”错误?

How do I resolve the "'mathfilter' tag 'addition' stopped working since replicating django project" error?

最近我将一个 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

Then add mathfilters to your INSTALLED_APPS.

我也在项目settings.py中检查了INSTALLED_APPS并且mathfilters也被加载了:

INSTALLED_APPS = [
...
'mathfilters',
...

]

在问题模板的顶部 'mathfilters' 已加载:

{% extends "base.html" %}
{% load i18n %}
{% load static %}
{% load extra %}
{% load mathfilters %}

如果我将模板中的 'addition' 更改为 'add',则模板工作正常。

{% with deeb_percent=stat.deeb_base|addition:stat.deeb_deal %}

但我不想在多个地方对每个模板都这样做,而且离开这个 'broken' 也很不舒服,因为我在新的 virtualenv 中安装了项目。

为什么数学过滤器不起作用?我该如何解决这个错误?

https://github.com/dbrgn/django-mathfilters

addition – replacement for the add filter with support for float / decimal types

将 mathfilter 更新到 0.4.0,一切都会恢复正常

对于Python3,只需使用{{ a|add:b}}