首页 > 解决方案 > The foreign key id cannot loaded in drop-down list in Django temlate

问题描述

In the entry.id cannot fetch the data from table. And I also save the selected option to store in another table. How can I do this?

register.html

<form method="POST"> 
<select name="item_id"> 
{% for entry in items %} 
<option value="{{ entry.id }}">{{ entry.name }}</option> 
{% endfor %} 
</select> 
</form>

标签: python-3.xdatabasedjango-modelsdjango-formsdjango-templates

解决方案


只需传递条目名称而不是选项标签

<form method="POST"> 
<select name="item_id"> 
{% for entry in items %} 

{{ entry.name }}

{% endfor %} 
</select> 
</form>

它将自动采用选项标签


推荐阅读