python - Django for循环显示数据库中的所有对象

标签 python html mysql django sqlite

def homepage(response):
    data = Project_types.objects.all()
    return render(response, 'main/homepage.html',{'projects':data})


def hacks(response):
    return render(response, 'main/hacks.html', {})

def games(response):
    return render(response, 'main/games.html', {})

我需要知道的是如何遍历html中变量“data”中的每个对象。我希望它以最简单的方式显示!

最佳答案

使用 FOR 模板标签,您可以遍历数据列表中的对象 ...

模板名称.html:

{% for iteratorname in projects %}
    <h1> {{ iteratorname.attributes }} </h1>
{% endfor %}

Please read the documentation for more details

关于python - Django for循环显示数据库中的所有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69804058/

相关文章:

html - 如何让 flex 中的 child 全宽?

mysql - 向 mySQL GROUP_CONCAT 语句添加内部连接

sql - 从 2 个表中计算高分排名

python - 绘制树状图,根节点在顶部

python - 用循环覆盖一个类的几个方法

python - 在 MacOS 上安装和配置最新的 python 3.8 时出现问题

python - 如何使 FunctionTransformer 在 DataFrameMapper 中工作

html - 如何使用 HTML/JavaScript 强制下载?

javascript - 带有选择/取消选择和汇总选项的表格

mysql - 同一个表的子选择查询