vendredi 13 mars 2020

Django View Error: Local variable referenced before assignment -> Flaw in if-statement?

In my Django view I have the following if-loop:

for i in queryset:

    if i['mnemonic'] == '#0602###95EUR00':
        cash_ps_totbank_eur = i['value'] 
        cash_ps_totbank_eur_sub1 = i['value']

    if i['mnemonic'] == '#0602###90EUR00':
        cash_ps_totbank_eur += i['value']        # Working
        cash_ps_totbank_eur_sub2 = i['value']

    if i['mnemonic'] == '#0602###95USD00':
        cash_ps_totbank_usd = i['value']
        cash_ps_totbank_usd_sub1 = i['value']

    if i['mnemonic'] == '#0602###2095900':
        cash_ps_totbank_usd += i['value']       # NOT working & throwing error
        cash_ps_totbank_usd_sub2 = i['value']

When loading the template, Django throws me an error saying local variable 'cash_ps_totbank_usd' referenced before assignment. I do the same thing here as eight lines above, namely adding a value to a varible that was initiated in the if-loop before and hence should have already a value assigned to it.

With the variables ending on _sub1 and _sub2 i tried to check, if there is maybe an error with the equality-check between the queryset and the string. But this is not the case. The variables with _sub1 and _sub2 work perfectly fine and get the correct values assigned to.

Any idea what I am missing here?

Aucun commentaire:

Enregistrer un commentaire