user8634222 user8634222 - 23 days ago 8
Javascript Question

I did not show big drill down button

I did not show large content drill down button.
I load json file and the content of the file become drill down button's content.But now only small content drill down buttons are shown.json is like

{'items': {'---': '---',
‘A’: ‘a’,
‘B’: ‘b’,
‘C: ‘c’,
‘D’: ‘d’},
'type1':
{
"---":"---",
"a1":"a1",
"a2":"a2",
"a3":"a3",
"a4":"a4"
},
'type2':
{
"---":"---",
"b1":"b1",
"b2":"b2",
"b3":"b3",
"b4":"b4"
},

'type3':
{
"---":"---",
"c1":"c1",
"c2":"c2",
},

'type4':
{
"---":"---",
"d1":"d1",
"d2":"d2",
"d3":"d3"
},
}


views.py is

from collections import OrderedDict
from django.shortcuts import render
import json
from django.http import JsonResponse

def index(request):
with open('./data/company_demand.json', 'r') as f:
json_data = json.loads(f.read(), object_pairs_hook=OrderedDict)

preprocessed = []
counter = 0
for key in ["type1", "type2", "type3", "type4"]:
values = [(i + counter, value) for i, value in enumerate(json_data[key].values())]
preprocessed.append((key, values))
counter = len(json_data[key])
return render(request, 'index.html', {'json_data': json_data}, {'preprocessed': preprocessed})


index.html is

<select id="mainDD" data-placeholder="Choose" class="chzn-select" style="width:600px;">
{% for i in json_data.items.values %}
<option>{{ i }}</option>
{% endfor %}
</select>
{% for key, values in preprocessed %}
<select name="type" id="{{ key }}">
{% for counter, value in values %}
<option value="{{ counter }}">{{ value }}-{{counter}}</option>
{% endfor %}
</select>
{% endfor %}


But now this part

<select id="mainDD" data-placeholder="Choose" class="chzn-select" style="width:600px;">
{% for i in json_data.items.values %}
<option>{{ i }}</option>
{% endfor %}
</select>


is not shown.Why does such a error happen?How should I fix this?

Answer Source

json_data and preprocessed should be in the same dictionary passed to your template.

Change

return render(request, 'index.html', {'json_data': json_data}, {'preprocessed': preprocessed})

To

return render(request, 'index.html', {'json_data': json_data, 'preprocessed': preprocessed})