0

First I must admit that I'm not an expert in either python or in the django template (jinja) language so if I missed something Im sorry about that.

I wrote a simple python dict to hold additional data which has the following code:

    all_dividend_tx_additional_info[company] = 
        {'date': dividend.time_of_payment,
         'shares': dividend.shares_owned,
         'amount': dividend.amount,
         'currency': dividend.currency,
         'total_amount': total_amount,
         'tax': tax,
         'payout': payout
}

The company in the index for the dict will be filled out by the real company name in a for loop, but basically this is how it looks:

{u'BAKKA': {u'amount': Decimal('15.00'),
            u'currency': u'NOK',
            u'date': datetime.datetime(2018, 12, 4, 16, 0, 22, tzinfo=<UTC>),
            u'payout': 180.0,
            u'shares': 12,
            u'tax': 55.080000000000005,
            u'total_amount': Decimal('180.00')}}

However when I try to iterate over this like so:

for key, value in all_dividend_tx_additional_info.iteritems:
  print key, value

I get the following error: 'builtin_function_or_method' object is not iterable

However, when I try to use this dictionary and send it off to a template it works nicely with the following syntax:

{% for key, value in all_dividend_tx_additional_info.items %}
  {{ key }} - {{ value }}

Can someone explain why its iterable in a django template compared to python code ? Clearly there are differences here which I have probably missed, but I cant seem to figure out why.

exceed
  • 459
  • 7
  • 19
  • 1
    because templates are *not* python code. it just borrows some of the helpful declarators that looks like python. In your case, you forgot the function declaration `iteritems()`, you're trying to iterate over a function instead of the result of that function. – Rocky Li Dec 04 '18 at 16:55
  • The template engine implicitly calls callables. `dict.items` in a template correpsonds to `dict.items()` in Python. – user2390182 Dec 04 '18 at 16:55
  • This is what you are looking for: https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops – Herbert Dec 04 '18 at 16:55
  • Ah, of course, I forgot the () at the end of iteritems. Cant believe that I keep missing the small stuff but am checking everything else i can think of. Ive iterated over dicts so many times that i guess i didnt think that would be the problem. Thank you. I'll also check the answer that was linked to. I do know that django templates are not python code, but thought that it should fail to handle the same type of data.. but if it calls the right functions implicitly then it works. – exceed Dec 04 '18 at 17:01

2 Answers2

0

iteritems is the method (the function) for a dictionary. It has no parameters, but to applying it to obtain (key, value) pairs you have to add parentheses:

for key, value in all_dividend_tx_additional_info.iteritems():
MarianD
  • 13,096
  • 12
  • 42
  • 54
  • Thanks, that worked. Ive typed this many times before but i forgot the () for some reason, annoying to forget such a small thing. – exceed Dec 04 '18 at 17:05
0

You Should Try This for Python 3.x

for key, value in all_dividend_tx_additional_info.items():
    print(key, value)

For Python 2.x Try iteritems()

for key, value in all_dividend_tx_additional_info.iteritems():
        print(key, value)
Ankit Singh
  • 247
  • 4
  • 14
  • Currently im still using python 2.7 but will some day get to 3.x. thanks anyway. – exceed Dec 04 '18 at 17:05
  • check for 2.x version also , i just edited my answer and if u like and it worked for u thn feel free to vote my answer and All the very best for your project !!! – Ankit Singh Dec 04 '18 at 17:08