2017-07-13 1 views
0

テンプレートタグを使用して、バックエンドのクラスオブジェクトから派生したデータを表示しています。両方のクラスオブジェクトは、ユーザーが作業するための入力を必要とし、入力が有効でない場合、Noneを返します。Django 1.11でTemplate Tagを使用して "NoneType"エラーを回避することはできますか?

2つのオブジェクトの差異計算をページに表示する必要があります(データベースに保存する必要はありません)。だから私は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> 

は同じエラーを受信しました。

テンプレートタグとこの問題を解決するフィルタがありますか?

答えて

2

"int_goals"は定義されていないと思われます。 int_numbers.get_sum_int_daily_numbers - None.get_div_goalsint_wdaysで数値演算を実行しようとすると、Pythonは例外をスローします

+0

{%if int_goals%} {{var | sub:}} {%endif%}は機能しました! –

関連する問題