2017-10-19 5 views
0

저는 jinga2 템플릿과 Json을 사용하여 플라스크 웹 앱을 설정했습니다. 내 HTML 템플릿 json 파일에 저장된 데이터를 표시하려면 노력하고있어. 이상적으로는 html로 표시하고 목록처럼 보이기를 원합니다. 페이지에 json이 레서피 형식으로 표시되어야합니다 (예 : 내 단계를 수행하십시오). 작동시키지 못합니다.jsonja2 템플릿을 사용하여 json을 표시합니다.

문제를 재현하는 데 도움이되는 파이썬 파일, json 및 html 템플릿이 포함되었습니다.

{ 
"name": "pumpkin pie", 
"ingredients": [" large eggs plus 1 yolk", "1 tsp ground cinnamon"], 
"methods": { 
    "1": "Pre-heat the oven to 200C/400F/Gas 6", 
    "2": "If using a shop bought sweet crust pastry case, use one that is 23cm/9in diameter and 4cm/1.5in deep. If using your own pastry, roll it out and use it to line a 23cm/9in pie plate (not loose bottomed). Bake the pastry case blind for 20 minutes." 
} } 
+0

: 당신은 같은 출력을 얻을 것이다

? – thaavik

+0

이렇게 json 방법을 표시하고 여러 번 표시합니다. u'1 ': u'200C/400F/Gas 6', u'2 '로 오븐을 준비하십시오. 직경 23cm/9in, 깊이 4cm/1.5in 인 것을 사용하십시오. 자신의 패스트리를 사용하는 경우 그것을 밖으로 굴려 23cm/9in 원형 판 (줄이 바닥이 아닌)에 줄을 서서 사용하십시오. 페스트리 케이스를 20 분 동안 장님으로 구우십시오. '} –

답변

0

JSON을에서 : 내 JSON 파일이

<div class="container"> 
<div class="ingredients"> 
    <p>{{ pumpkin.ingredients }}</p> 
</div> 
<div class="method"> 
    <ul> 
    {% for result in pumpkin %} 
     {% for methods in result %} 
      <li>{{pumpkin.methods }}</li> 
     {% endfor %} 
    {% endfor %} 
    </ul> 
</div> 

입니다 : 이것은 내 html로 tempate 파일입니다

@app.route('/recipes/pumpkin_pie/') 
@app.route('/recipes/halloween/pumpkin_pie/') 
def pumpkin_pie(): 
    recipes = [] 
    with open('recipes.json', 'r') as f: 
    recipes = json.load(f) 
    f.close() 
    print recipes 

    p = {} 
    for item in recipes: 
    if item['name'] == "pumpkin pie": 
     print item 
     p = item 
     print p 

    return render_template('pumpkin_pie.html', pumpkin=p) 

:

내 파이썬 파일입니다 당신이하지 않는 파일 객체의 배열을 가진다. 오히려 하나의 객체 만 있습니다. 따라서 파이썬에서 객체의 배열로 가정하면 iterating을하면 원하는 결과를 얻을 수 없습니다.

업데이트 파이썬 파일로 :

@app.route('/recipes/pumpkin_pie/') 
@app.route('/recipes/halloween/pumpkin_pie/') 
def pumpkin_pie(): 
    recipes = [] 
    with open('recipes.json', 'r') as f: 
     recipes = json.load(f) 
     f.close() 
    return render_template('pumpkin_pie.html', pumpkin=recipes) 

과 같은 템플릿 파일 : 정확히 작동하지 않습니다 무엇

enter image description here