javascriptjquerydjangofiletree

Loading file with JavaScript inside a Django app


I am using a JavaScript (jQuery FileTree, see here: http://www.abeautifulsite.net/blog/2008/03/jquery-file-tree/) inside my Django application. The problem is, this JavaScript needs the path to a python script. How can I realise this? I can not use Django template tags inside JavaScript.

In my html template I have:

    {% load staticfiles %}
    <script src={% static "treeview.js" %} type="text/javascript"></script>

And this is the JavaScript treeview.js (which is loaded correctly):

    $(document).ready(function() {

        $('#file_selector').fileTree({
        root: '/',

        // how to load this file? 
        script: 'path/to/python/script.py',

        folderEvent: 'click', expandSpeed: 750, collapseSpeed: 750,
        expandEasing: 'easeOutBounce',
        collapseEasing: 'easeOutBounce', loadMessage: 'Laden...',
        multiFolder: false},

        function(file) { alert(file); }
        );

    });

Django will not find the script path/to/python/script.py, how can I serve this file? Maybe I have not yet fully understood the concept of static files in Django? Note that the script doesn't have to be python, I want to know how to load an arbitrary file inside my JavaScript.


Solution

  • You've misunderstood what the "script" parameter is. It's not a file path to a script, it's a URL: the location of the server-side code that returns the JSON to populate the treeview. You need to write a view and connect it with your urlconf, then use that URL in the JS options.

    Note that that package apparently already includes a connector for Django. Also you might want to reflect that there are probably better options for a file browser than an unmaintainted six-year-old package.