[英]How can i enter value into select/option from django model?
這是我在這里的第一個問題。
我有一個看起來像這樣的模型;
class Fund(models.Model):
name = models.CharField(max_length=500, null=False, blank=False)
code = models.CharField(max_length=5, null=False, blank=False)
management_price = models.CharField(max_length=15, null=True, blank=True)
performance_price = models.CharField(max_length=15, null=True, blank=True)
explanation = models.TextField(max_length=1000, null=True, blank=True)
我想在此處模板的選定區域中顯示所有基金名稱;
<div class="form-group col-md-12">
<label>Fon*</label>
<select id="fundIdSelect" class="form-control select2">
<option value="0">Lütfen Fon Seçiniz</option>
</select>
</div>
這是我相關的 views.py;
def privacy(request):
fund_list = Fund.objects.all()
return render(request, 'privacy.html', context={'fund_list': fund_list})
您可以在模板中迭代查詢集。 (我假設你想要下拉菜單中的名字。)
<div class="form-group col-md-12">
<label>Fon*</label>
<select id="fundIdSelect" class="form-control select2">
{% for i in fund_list %}
<option value="{{ i.name }}">{{ i.name }}</option>
{% endfor %}
</select>
</div>
您假設在您的privacy.html
文件中使用 div 標簽運行一個 for 循環,如下所示:
{% for funds in fund_list %}
{{ funds }}
{% endfor %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.