2017-07-13 4 views
0

Ich verwende Schablonen-Tags, um Daten darzustellen, die von Klassenobjekten am Back-End abgeleitet wurden. Beide Klassenobjekte erfordern Eingaben vom Benutzer, um zu arbeiten, und wenn die Eingabe nicht gültig ist, geben sie None zurück.Ist es möglich, Vorlagen-Tags in Django 1.11 zu verwenden, um einen "NoneType" -Fehler zu umgehen?

Ich muss eine Differenzberechnung von zwei der Objekte auf der Seite (es muss nicht in der Datenbank gespeichert werden). Also habe ich mathfilters https://pypi.python.org/pypi/django-mathfilters installiert, um die Subtraktion zu machen, die funktionierte, wenn es Benutzereingaben gibt, aber nicht funktioniert, wenn ich gerade zur Seite oder keiner Benutzereingabe navigiere.

Vorlage 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> 

Welche mir diesen Fehler gibt:

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' 

ich diese Filter versucht:

<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> 

den gleichen Fehler empfangen.

Gibt es einen Weg mit Vorlage Tags und Filtern, um dieses Problem zu beheben?

Antwort

2

Scheint, dass "int_goals" nicht definiert ist, also keine. Wenn Sie versuchen, eine mathematische Operation auf int_numbers.get_sum_int_daily_numbers - None.get_div_goalsint_wdaysint_numbers.get_sum_int_daily_numbers - None.get_div_goalsint_wdays zu tun, wirft eine Ausnahme

+0

Die {% if int_goals%} {{var | sub:}} {% endif%} gearbeitet! –

Verwandte Themen