【问题标题】:Showing Django model field in HTML在 HTML 中显示 Django 模型字段
【发布时间】:2013-04-20 07:19:00
【问题描述】:

我想知道如何在 HTML 输出中显示我拥有的字段。

我的 model.py 中有这个类

class Tower(models.Model):
    ap = models.IntegerField()

我想将 ap 保存的值显示到 HTML 输出中,这就是我目前在 html 文档中的内容。

{% for a in tower.tower_set.all %}
AP: {{a.ap}}<br>
{%endfor%}

【问题讨论】:

  • 那有什么问题?
  • @DanielRoseman :tower.tower_set.all 毫无意义。这是不对的。

标签: django class models field


【解决方案1】:

希望你也有这样的看法:

from django.template.response import TemplateResponse
from models import Tower

def tower_list(request):
  return TemplateResponse(request, "your_template.html",
                          {"towers": tower.objects.all()})

您的模板“your_template.html”应如下所示:

{% for a in towers %}
  AP: {{a.ap}}<br>
{%endfor%}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-23
    • 2013-08-28
    • 2013-07-16
    • 2018-10-22
    • 2016-11-19
    • 1970-01-01
    • 2021-12-07
    • 2011-03-07
    相关资源
    最近更新 更多