views:

23

answers:

1

Hi,

So I've got a expanding/collapsing tree that I'm keeping track of in session data.

I want to be able to render the tree in Django based upon the session data.

I am saving the state with something like:

request.session['treedata'][item_id] = state # (0 or 1)

In my template for rendering, I'm looping through the items and for each item I want to set the visibility of the object for example:

{% for item in itemlist %} 
<div {% if request.session.treedata.<whatgoeshere?> %}style="display:none"{% endif %}>
Content of the subtree
</div>
{% endfor %}

So the is where I'm confused.

Can I specify:

request.session.treedata.(item.id)

or

request.session.treedata.(forloop.counter)

Or do I have to preprocess the item and the state into a new context variable?

Thanks!

James

A: 

Assuming that item is a model, and item_id is item.id, you should be able to do:

{% for item in itemlist %} 
  <div {% if request.session.treedata.item.id %}style="display:none"{% endif %}>
    Content of the subtree
  </div>
{% endfor %}

You can read about how Django templates looking variables here:

http://docs.djangoproject.com/en/1.2/topics/templates/#variables

brianz
I ended up shoving the session data into the instances of the models so that I could loop on the items and then do item.expanded. item.save() doesn't have a problem with extra data it seems. But this approach is far more likeable. Let's see, the treedata is a dictionary. But will it then use the "item" from the loop? I just tried to get this to work. I could get nothing unless the (item.id) was actually just a number. In other words, the only thing that worked was request.session.treedata.1 Is there a way around that one? I couldn't even use with item.id as num.
iJames