Displaying a Tree view in template using django-mptt

1,275 views
Skip to first unread message

Manakel

unread,
Feb 25, 2008, 8:07:38 AM2/25/08
to Django users
Hello Dear all,

First let me say that i'm very new in term of web based developpemnt
(the HTML/CSS Part). i've always worked on backend logic (in java,
python, etc....) never on the end user interface directly.

This said, i'm trying to use the django-mptt to install a simple
hierarchy in my first django app.
And I'm blocked at the view template / level
(ie rendering something for the end user)

At the model level, the integration is successful.
i've registered my Item model in mptt.
My Item has a 'parent' field that is used by mptt to discover parent/
child relation ship.

So i can create some Item hierarchy in a breeze....And i can use it on
the python command line.

But i can't figure my self how to integrate this in a template.
What i'm looking for is a way to render the whole item Tree.
Each item would have it's own line with it's title
Each item would be a little more on the right side depending on the
depth in the hiearchy
Each item would have a link on the right to edit himself
Each item would have a link on the right to move himself

So i guess i should use the mptt template tag
{% full_tree_for_model myApp.Item as OneItem %}

But then, how do i display for each OneItem
1) OneItem deppth in the tree
2) OneItem title
3) OneItem pk id (to generate the right link)


Thomas Guettler

unread,
Feb 26, 2008, 4:20:42 AM2/26/08
to django...@googlegroups.com
> But i can't figure my self how to integrate this in a template.
> What i'm looking for is a way to render the whole item Tree.
>
> Each item would have it's own line with it's title
> Each item would be a little more on the right side depending on the
> depth in the hiearchy
> Each item would have a link on the right to edit himself
> Each item would have a link on the right to move himself
>
> So i guess i should use the mptt template tag
> {% full_tree_for_model myApp.Item as OneItem %}
>
> But then, how do i display for each OneItem
> 1) OneItem deppth in the tree
> 2) OneItem title
> 3) OneItem pk id (to generate the right link)
Hi,
I use a javascript library to display the tree:
http://developer.yahoo.com/yui/treeview/

It works quite good, even lazy loading (via JSON) is supported.
If you are new to javascript it will take you some hours, to get
it right.

The drawback is, that the user must have javascript enabled, otherwise
he can't see the tree (even the content which is inside the HTML file). At
least that's how I did it.

Some hints:

- You need to use escapejs for the initial data which can be stored in
JSON format in the HTML.
http://www.djangoproject.com/documentation/templates/#escapejs

- You need to write a view which returns JSON and you need a javascript
function which inserts the data into the tree.

BTW, I used a simple method for storing the hierarchical data: The
table has
a column which references itself. If this column is NULL, this
row/object has no
parent (root object).

Thomas


--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de

Reply all
Reply to author
Forward
0 new messages