2017-09-26 63 views
-1

我沒有顯示大內容鑽取按鈕。 我加載JSON文件,成爲檔案的內容鑽取按鈕的content.But現在只有少量的內容鑽取按鈕shown.json就像我沒有顯示大鑽取按鈕

{'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是

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}) 

指數html的是

<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 %} 

但現在這部分

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

沒有顯示。爲什麼會發生這樣的錯誤?我應該如何解決這個問題?

回答

0

json_data和預處理應該在傳遞給你的模板的同一個字典中。

變化

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

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