【问题标题】:Unable to view database object in django in html无法在 html 中查看 django 中的数据库对象
【发布时间】: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>

数据库值

Table values

但在网页中没有显示这些值

Form

我是 django 新手,我需要帮助。我在这里做错了什么吗?

【问题讨论】:

  • 你的上下文说'outform',而你的模板有'outfrom' - 这可能就是原因

标签: python html django


【解决方案1】:

看起来你有一个错字,你在上下文中命名变量(outform),但在你的模板中调用(outfrom)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-26
    • 2020-08-22
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多