是否可以在Django 1.11中使用模板标签来解决“NoneType”错误?

时间:2017-07-13 13:00:07

标签: django-template-filters

我使用模板标签来呈现从后端的类对象派生的数据。两个类对​​象都需要用户输入才能工作,如果输入无效,则返回None。

我需要在页面上显示两个对象的差异计算(它不需要保存到数据库中)。所以我已经安装了mathfilters https://pypi.python.org/pypi/django-mathfilters来进行减法,如果有用户输入则该减法有效但如果我只是导航到页面或没有用户输入则不起作用。

模板HTML:

<tr>
   <td>Interest</td>
   <td class="data">${{int_numbers.get_sum_int_daily_numbers  | intcomma }}</td>
   <td class="data">${{int_goals.get_div_goalsint_wdays | intcomma }}</td>
   <td class="data"><font class="red">
    ${{ int_numbers.get_sum_int_daily_numbers|sub:int_goals.get_div_goalsint_wdays | intcomma }}
                </font>
   </td>
</tr>

这给了我这个错误:

Traceback:

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in _resolve_lookup
  882.                     current = current[bit]

During handling of the above exception ('NoneType' object is not subscriptable), another exception occurred:

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in _resolve_lookup
  890.                         current = getattr(current, bit)

During handling of the above exception ('NoneType' object has no attribute 'get_div_goalsint_wdays'), another exception occurred:

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in _resolve_lookup
  896.                             current = current[int(bit)]

During handling of the above exception (invalid literal for int() with base 10: 'get_div_goalsint_wdays'), another exception occurred:

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\core\handlers\exception.py" in inner
  41.             response = get_response(request)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\DevProj\am\amreports\reports\views.py" in goals_view
  61.         'int_monthtodate': int_monthtodate_goals,

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\shortcuts.py" in render
  30.     content = loader.render_to_string(template_name, context, request, using=using)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\loader.py" in render_to_string
  68.     return template.render(context, request)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\backends\django.py" in render
  66.             return self.template.render(context)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in render
  207.                     return self._render(context)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in _render
  199.         return self.nodelist.render(context)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in render
  990.                 bit = node.render_annotated(context)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in render_annotated
  957.             return self.render(context)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\loader_tags.py" in render
  177.             return compiled_parent._render(context)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in _render
  199.         return self.nodelist.render(context)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in render
  990.                 bit = node.render_annotated(context)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in render_annotated
  957.             return self.render(context)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\loader_tags.py" in render
  72.                 result = block.nodelist.render(context)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in render
  990.                 bit = node.render_annotated(context)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in render_annotated
  957.             return self.render(context)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in render
  1040.             output = self.filter_expression.resolve(context)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in resolve
  730.                     arg_vals.append(arg.resolve(context))

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in resolve
  849.             value = self._resolve_lookup(context)

File "C:\pycharm-virtenv\DjangoPostgres\lib\site-packages\django\template\base.py" in _resolve_lookup
  903.                                                        (bit, current))  # missing attribute

Exception Type: VariableDoesNotExist at /reports/goals/
Exception Value: Failed lookup for key [get_div_goalsint_wdays] in 'None'

我试过这个过滤器:

<tr>
   <td>Interest</td>
   <td class="data">${{int_numbers.get_sum_int_daily_numbers  | intcomma }}</td>
   <td class="data">${{int_goals.get_div_goalsint_wdays | intcomma }}</td>                  
    <td class="data"><font class="red">
        {% if int_numbers.get_sum_int_daily_numbers|sub:int_goals.get_div_goalsint_wdays is "None" %}
        0
        {% else %}
        ${{ int_numbers.get_sum_int_daily_numbers|sub:int_goals.get_div_goalsint_wdays | intcomma }}
         {% endif %}
                    </font>
    </td>
</tr>

收到同样的错误。

是否有办法使用模板标签和过滤器来解决此问题?

1 个答案:

答案 0 :(得分:2)

似乎“int_goals”未定义,因此无。如果您尝试进行数学运算 int_numbers.get_sum_int_daily_numbers - None.get_div_goalsint_wdays python将抛出异常