On 09/07/12 13:06, Jason wrote:
> I'm using django 1.2 and am attempting to get the following code to work.
>
> private_folder_details = [{"folderId":"1111", "name": "The folder
> name"},{"folderId":"1221", "name": "The other folder name"}]
> private_folders = [{"id":"1111"},{"id":"2222"}]
>
> {% for folder in private_folders %}
> * {% if
folder.id in private_folder_details %}*
> <input type="checkbox" name="syncCheckbox" id="{{
folder.id}}"
> checked="yes" value="{{
folder.id}}">
> {% else %}
> <input type="checkbox" name="syncCheckbox" id="{{
folder.id}}"
> value="{{
folder.id}}">
> {% endif %}
> {% endfor %}
>
> Essentially if
folder.id is within any of the private_folder_details
> folderId's then I want the checkbox to be checked. What I'm doing above
> doesn't seem to work though - any idea how it could be made to work?
A couple ideas occur to me, depending on how malleable your
underlying data-structure is and how much overlap there is between
them. My first thought is to change the private_folder_details to a
folderID->details mapping, something like
private_folder_details = [
{"folderId":"1111", "name": "The folder name"},
{"folderId":"1221", "name": "The other folder name"},
]
private_folder_details_1 = dict(
(d["folderId"], d)
for d in private_folder_details
)
private_folder_details_2 = set(
d["folderId"]
for d in private_folder_details
)
If all you need is the determination of whether a folder is/isn't a
private folder, use the second (set) version; if you need additional
information, use the former (dict) version. You can then test
whether the ID is in the set/dict, rather than if it's in the
list-of-dicts.
If for some reason you have multiple instances of the same folder-ID
in your list-of-dicts, you have to decide *why*, but should still be
able to use the second "set" version to gather folder-IDs of the
private folders.
-tkc