【发布时间】:2018-08-06 15:05:37
【问题描述】:
所以我有以下代码:
views.py:
def topics(request):
"""Show all topics"""
topics = Topic.objects.order_by('date_added')
context = {'topics': topics}
return render(request, 'learning_logs/topics.html', context)
return render(request, 'learning_logs/topic.html', context)
我知道我正在查询数据库并按日期对数据进行排序,然后将该数据存储在主题变量中。我的第一个问题是,主题变量是否将数据存储为列表? (假设有多个条目)。
如果是这样,当我的 html 文件中有以下代码时
topics.html:
<ul>
{% for topic in topics %}
<li>{{ topic }}</li>
{% empty %}
<li>No topics have been added yet.</li>
{% endfor %}
</ul>
为什么我需要将存储在主题中的数据传递到上下文字典中,以便主题.html 循环并显示数据?为什么不循环遍历主题变量本身?我只是对“上下文”的使用感到困惑
提前感谢您的帮助。
【问题讨论】:
标签: python html django dictionary url