2017-09-05 70 views
0

实际上,我不知道如何访问模板中的jsonfield数据。我想是这样的django无法访问模板中的jsonfield数据

{% for key in place.places_to_visit %} 
    {{ key }}:{{ value }} 
{% endfor %} 

但它表明我已经加入包括{}和“”,也同为人物 的Arrayfield我也想在jsonfield,添加图像如何将所有JSON数据去做?该图像是否需要成为媒体目录,然后我应该包含它的路径?或者其他一些方法来做到这一点? 它也呈现出如下places_to访问

"[05/Sep/2017 23:03:00] "GET /ontheway/1/ HTTP/1.1" 200 4238" 

jsonfield数据错误是

"Places to visit": [ 
      { 
       "name" : "some name", 
       "overview" : " some data", 
       "iamge": "image path" 
      } 
      { 
       "name" : "some name", 
       "Overview": " some data", 
       "image": "image path" 
      } 
      ] 

预先感谢您

回答

0

,你可以尝试使用items

{% for data in place.places_to_visit %} 
    {% for key, value in data.items %} 
     {% if key == 'image' %} 
      <img src="{{ value }}"> 
     {% else %} 
      <div> {{ key }}:{{ value }} </div> 
     {% endif %} 
    {% endfor %} 
{% endfor %} 
+0

不工作甚至没有显示它在网页上 –

+0

可以你的节目' place.places_to_visit'只需简单地在模板中添加{{place.places_to_visit}}',请将结果以文本格式添加到问题中,编辑它 –

+0

@pratikjaiswal更新了答案 –