0

次のような Python 辞書があります。

{'test_data': ['reads_1.fq', 'reads_2.fq'], 'test_data/new_directory': ['ok.txt'], 'hello': ['ok.txt'], 'hello/hi/hey': ['b.txt']}

テンプレート(Django)でこれを使用して、次のようなツリー構造を作成したい:

test_data
 reads_1.fq
 reads_2.fq

test_data/new_directory
 ok.txt

hello
 ok.txt

hello/hi/hey
 b.txt

javascriptを使用してこれを行うにはどうすればよいですか? ありがとう

4

1 に答える 1

2

ビューで最初に辞書をjsonに変換し、javascriptを使用してjsonで直接作業します(djangoのテンプレート言語を使用してjavascriptを試してフォーマットするのではなく)

# In your view somewhere
import json

def my_view(request):
    ...
    return render_to_response('my_template.html',{ 'my_json': json.dumps(my_dict) }, context_instance=RequestContext(request))

そしてあなたのテンプレートで:

<script>
     obj = {{ my_json|safe }};
     for (var prop in obj){  // Iterating through an object
         console.log(prop);
         for(i=0, i < obj[prop].length, i++){  // Iterating through the list for each key
              console.log(obj[prop][i]);
         }
     }
</script>
于 2013-06-17T10:22:37.077 に答える