将字典值放入 Django 模板中的 table
putting dictionary values inside a table in Django template
这是我第一次使用 Django 模板。我正在尝试使用 API.
构建的字典中的值在我的页面中创建一个 table
以下是我在 views.py 中发送数据的方式:
data = {
"year_most_launches": result_launches,
"launch_sites":response_sites,
"launches_2019_2021":response_2019_2021
}
return render(request,"main/launches.html", {"data":data})
这是我的 html 页面:
<table class="table">
<thead>
<tr>
<th>Year with most launches</th>
<th>Launch site with most launches</th>
<th>Number of launches between 2019 and 2021</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ data["year_most_launches"] }}</td>
<td>{{ data["launch_sites"] }}</td>
<td>{{ data["launches_2019_2021"] }}</td>
</tr>
</tbody>
</table>
但是我收到以下错误django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '["year_most_launches"]' from 'data["year_most_launches"]'
我该如何解决这个问题?有人可以帮忙吗?
Edit1:尝试只使用 {{ key }} 而不是 data["key"] 但现在当我渲染 html
时它没有出现
<table class="table">
<thead>
<tr>
<th>Year with most launches</th>
<th>Launch site with most launches</th>
<th>Number of launches between 2019 and 2021</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ year_most_launches }}</td>
<td>{{ launch_sites }}</td>
<td>{{ launches_2019_2021 }}</td>
</tr>
</tbody>
</table>
这是我第一次使用 Django 模板。我正在尝试使用 API.
构建的字典中的值在我的页面中创建一个 table以下是我在 views.py 中发送数据的方式:
data = {
"year_most_launches": result_launches,
"launch_sites":response_sites,
"launches_2019_2021":response_2019_2021
}
return render(request,"main/launches.html", {"data":data})
这是我的 html 页面:
<table class="table">
<thead>
<tr>
<th>Year with most launches</th>
<th>Launch site with most launches</th>
<th>Number of launches between 2019 and 2021</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ data["year_most_launches"] }}</td>
<td>{{ data["launch_sites"] }}</td>
<td>{{ data["launches_2019_2021"] }}</td>
</tr>
</tbody>
</table>
但是我收到以下错误django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '["year_most_launches"]' from 'data["year_most_launches"]'
我该如何解决这个问题?有人可以帮忙吗?
Edit1:尝试只使用 {{ key }} 而不是 data["key"] 但现在当我渲染 html
时它没有出现 <table class="table">
<thead>
<tr>
<th>Year with most launches</th>
<th>Launch site with most launches</th>
<th>Number of launches between 2019 and 2021</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ year_most_launches }}</td>
<td>{{ launch_sites }}</td>
<td>{{ launches_2019_2021 }}</td>
</tr>
</tbody>
</table>