Changes between Initial Version and Version 1 of Ticket #12486


Ignore:
Timestamp:
Jan 2, 2010, 4:23:50 PM (15 years ago)
Author:
Luke Plant
Comment:

This is essentially the same thing as #1495, and the lookup filter from #577

I don't know what your exact use case for this is, but it doesn't really fit with the ethos of Django's templating language. (As Adrian said on another ticket, "out of scope"). The intention is that Django's templates can be read and written by non-programmers, so complex logic should be view code or custom template tags.

(BTW, you were using 'hash' instead of 'key', which was confusing. I've corrected it now)

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #12486

    • Property Resolutionwontfix
    • Property Status newclosed
    • Property Summary Accessing dict values via a value of a variable as hashAccessing dict values via a value of a variable as key
  • Ticket #12486 – Description

    initial v1  
    1 In the current release (and previous releases also) there is no way to access the value of a dict or other struct with the value of a variable as the hash. For example if you want to have more than one struct you want to iterate over and get a key-value-based table with the data of other structs in it, you either have to do it manually inside the normal python code or with very ugly filters.
    2 [[BR]][[BR]]
     1In the current release (and previous releases also) there is no way to access the value of a dict or other struct with the value of a variable as the key. For example if you want to have more than one struct you want to iterate over and get a key-value-based table with the data of other structs in it, you either have to do it manually inside the normal python code or with very ugly filters.
     2
    33Here is a simple example i mentioned before:
    44(we have dictionary1 and dictionary2 with the same size and keys)
     
    1010[[BR]]
    1111This wouldn't work because it would search for dictionary2.key or dictionary[ "key" ] (so key is the string "key", not a variable)
    12 [[BR]]But to get example working you would need it to call dictionary[ key ] whereas the key is a variable and its value would be used as a hash.
     12[[BR]]But to get example working you would need it to call dictionary[ key ] whereas the key is a variable and its value would be used as a key.
    1313Most solution with filters look this way:
    1414
    1515{{{
    1616@register.filter
    17 def hash(h, key):
    18     return h[key]
     17def lookup(d, key):
     18    return d[key]
    1919}}}
    2020(took this one from [http://push.cx/2007/django-template-tag-for-dictionary-access]
Back to Top