代码之家  ›  专栏  ›  技术社区  ›  Prof. Falken

可以在Django 1.11中使用模板标记来绕过“NoneType”错误吗?

  •  0
  • Prof. Falken  · 技术社区  · 7 年前

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

    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 回复  |  直到 7 年前
        1
  •  2
  •   stefan georg    7 年前

    似乎“int_目标”未定义,因此没有。如果你试着在上面做一个数学运算 int_numbers.get_sum_int_daily_numbers - None.get_div_goalsint_wdays python将引发异常