我将如何解析以将存储地址ID和标题从api加载到select选项值?
到目前为止我尝试过的
<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>
最佳答案
将模板更改为。
演示:
<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
的列表。