Peter Swanson

Software Engineer

Full Stack Web Developer


Representing Recursive Data on a Webserver



I run my webservers using the Django Python Web Framework.


Django is great because it allows you to easily display variables from a Python backend in the frontend HTML templates.


views.py

	              
def view_hierarchy(request):
    root = Root()
    root.populate("fs_demo")
    return render(request, 'base/view_hierarchy.html', {'root':root})
	              

The real challenge here is displaying the Root data structure (containing the recursively mapped directory information) with a template.



A great solution is to make your Django HTML templates similarly recursive. This solution mirrors our initial mapping exactly by using one root template to display a list of the top level files and directories.



view_hierarchy.html


The top level template inserts the recursive template for each directory. The same replacement happens in the recursive template for every lower level directory.


direc_contents.html


The result is a quick way to generate a perfectly preserved representation of any directory hierarchy on your server. See the demo!