2017-09-25 105 views
-1

我得到一個錯誤,無法解析餘數: '()'

TemplateSyntaxError at /app/ 
Could not parse the remainder: '():' from 'json_dict.items():'. 

我想嵌入json_parse方法的結果views.py到的index.html。 我views.py寫像

from django.shortcuts import render 
    import json 

    def index(request): 
     return render(request, './index.html') 

    def json_parse(): 
     f = open('./data/data.json', 'r') 
     json_dict = json.load(f) 

我在寫的index.html像

<html> 
 <head> 
 <script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script> 
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.8.2/chosen.jquery.min.js"></script> 
    <script src="//cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.js"></script> 
 <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.css"> 
 </head> 
 <body> 
    <select id="mainDD" data-placeholder="Choose" class="chzn-select" style="width:600px;"> 
    {% for k, v in json_dict.items(): %} 
     {% for i in range(len(k)) %} 
      <p>{{ i }}</p> 
      <p>{{ i }}</p> 
      <p>{{ i }}</p> 
      <p>{{ i }}</p> 
      <p>{{ i }}</p> 
     {% endfor %} 
    {% endfor %} 
    </select> 
    </body> 
</html> 

json_dict的是{'items': [{'---': '---', ‘A’: ‘a’, ‘B’: ‘b’, ‘C: ‘c’, ‘D’: ‘d’}]}。 我想嵌入--- & a & b & c & d在這個地方我變量的p標籤。 我沒有想到在我的代碼中發生語法錯誤,所以我真的不明白爲什麼會發生這個錯誤。我應該如何解決這個問題?我可以寫什麼?

+0

並刪除():,如果你刪除它說,它無法解析三個字符?例如,你在下一個循環中沒有冒號,所以目前還不清楚爲什麼你會認爲你需要這個冒號。 – jonrsharpe

+0

當然,下一個循環也不會工作,因爲'range(len(k))'不被允許。不是說你應該永遠在任何代碼中使用該構造。 –

回答

0

從模板

{% for k, v in json_dict.items %}