【发布时间】:2022-07-06 21:54:13
【问题描述】:
数据库中的对象在 html 中是不可见的。
型号
class OutFormatTemplate(models.Model):
title = models.CharField(max_length=80)
template = models.FileField(upload_to='out_put_temp/')
def __str__(self):
return f"{self.title}"
查看
def upload_form(request):
out_form = OutFormatTemplate.objects.all()
for i in out_form:
print(i.id, i.title)
form = UploadBookForm()
context = {
'form': form,
'outform': out_form
}
return render(request, 'file_uploader.html', context)
HTML
{% if outfrom %}
<p>Out Form Exist</p>
{% else %}
<p>Not Exist</p>
{% endif %}
<div class="mt-2" id="out-template">
<label for="template-out" class="form-label">Select Output Template</label>
<select class="form-select" aria-label="Default out select example" name="out_template_id" id="template-out">
<option value="0" selected></option>
{% for out in outfrom %}
<option value="{{ out.id }}">{{ out.title }}</option>
{% endfor %}
</select>
</div>
数据库值
但在网页中没有显示这些值
我是 django 新手,我需要帮助。我在这里做错了什么吗?
【问题讨论】:
-
你的上下文说'outform',而你的模板有'outfrom' - 这可能就是原因