我将如何解析它以将商店地址ID和标题从api加载到选择选项值?
到目前为止我尝试过的
<select>
{% for store in stores.store_address %}
<option value="{{ store.store_address.id}}">{{ store.store_address.title}}</option>
{% endfor %}
</select>
获取数据的代码
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
stores = requests.get('https://api.myjson.com/bins/16tdlw').json()
context['stores'] = stores
return context
URL:https://api.myjson.com/bins/16tdlw(json数据)
模板:
<select>
{% for store in stores.store_address %}
<option value="{{ store.store_address.id}}">{{ store.store_address.title}}</option>
{% endfor %}
</select>
答案 0 :(得分:1)
将模板更改为。
演示:
<select>
{% for store in stores %}
{% for store_addres in stores.store_address %}
<option value="{{ store_addres.id}}">{{ store_addres.title}}</option>
{% endfor %}
{% endfor %}
</select>
注意:
stores
是您需要对其进行迭代然后访问store_address
的列表。