for循环填充django模板table
for loop to populate a django template table
我正在尝试在 Django 模板中制作 table。我正在像这样在 views.py
中发送一个字典作为我的数据:
from posixpath import split
from unittest import result
from django.shortcuts import render
import requests
from .utils import most_frequent, get_json_values, get_max_dict
def launches(request):
"""main request. Retrieve the year that had most launches"""
response_launches =
requests.get('https://api.spacexdata.com/v3/launches?
filter=launch_year')
launches = response_launches.json()
launch_years = get_json_values('launch_year',launches)
result_launches = most_frequent(launch_years)
"""retrieve the launch site most used for launches """
response_sites =
requests.get('https://api.spacexdata.com/v3/launches?
filter=launch_site')
sites = response_sites.json()
launch_sites = get_json_values("launch_site", sites)
result_sites = get_max_dict(launch_sites,'site_id')
"""retrieve the number of launches between 2019 and 2021"""
response_2019_2021 =
requests.get('https://api.spacexdata.com/v3/launches?
start=2019&end=2021')
launches_2019_2021 = response_2019_2021.json()
result_2019_2021 = len(launches_2019_2021)
data = {
"year_most_launches": result_launches,
"launch_sites":result_sites,
"launches_2019_2021":result_2019_2021
}
print(result_launches,result_sites,result_2019_2021)
return render(request,"main/launches.html", {"data":data})
我的utils.py
:
from collections import Counter
from typing import Dict, List
def most_frequent(List) -> int:
"""return the most frequent element inside a list."""
occurence_count = Counter(List)
return occurence_count.most_common(1)[0][0]
def get_json_values(key,input_data) -> List:
"""return the values of dicts inside a list."""
return [sub_val[key] for sub_val in input_data if key in sub_val]
def max_dict_value(List,key) -> List:
"""return the key that is linked to the most frequent value."""
counts = dict()
for dictionary in List:
counts[dictionary[key]] = counts.get(dictionary[key],0) + 1
max_keys = [key for key, value in counts.items() if value ==
max(counts.values())]
return max_keys
def get_max_dict(List,key) -> List:
"""return the dictionary that maps to the max_dict_value."""
for dictionary in List:
if dictionary[key] == max_dict_value(List,key)[0]:
return dictionary
我的 table 在 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>
{% for element in data.values %}
<tr>
<td>{{ element }}</td>
</tr>
{% endfor %}
</tbody>
</table>
我的问题是数据值只出现在第一列,创建 3 行而不是只出现在第一行。
我该如何解决?这是我 html?
内部的问题
您的 HTML table 正在为每个元素创建一个新行 (<tr>
),您想要的是为每个元素创建一个新单元格 (<td>
)每个元素,像这样:
<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>
{% for element in data.values %}
<td>{{ element }}</td>
{% endfor %}
</tr>
</tbody>
</table>
当然,这只会产生一行,这可能是您想要的,但是如果您想要多行,则需要两个循环,一个外部循环用于每个 <tr>
,一个内部循环用于每个 <td>
.
我正在尝试在 Django 模板中制作 table。我正在像这样在 views.py
中发送一个字典作为我的数据:
from posixpath import split
from unittest import result
from django.shortcuts import render
import requests
from .utils import most_frequent, get_json_values, get_max_dict
def launches(request):
"""main request. Retrieve the year that had most launches"""
response_launches =
requests.get('https://api.spacexdata.com/v3/launches?
filter=launch_year')
launches = response_launches.json()
launch_years = get_json_values('launch_year',launches)
result_launches = most_frequent(launch_years)
"""retrieve the launch site most used for launches """
response_sites =
requests.get('https://api.spacexdata.com/v3/launches?
filter=launch_site')
sites = response_sites.json()
launch_sites = get_json_values("launch_site", sites)
result_sites = get_max_dict(launch_sites,'site_id')
"""retrieve the number of launches between 2019 and 2021"""
response_2019_2021 =
requests.get('https://api.spacexdata.com/v3/launches?
start=2019&end=2021')
launches_2019_2021 = response_2019_2021.json()
result_2019_2021 = len(launches_2019_2021)
data = {
"year_most_launches": result_launches,
"launch_sites":result_sites,
"launches_2019_2021":result_2019_2021
}
print(result_launches,result_sites,result_2019_2021)
return render(request,"main/launches.html", {"data":data})
我的utils.py
:
from collections import Counter
from typing import Dict, List
def most_frequent(List) -> int:
"""return the most frequent element inside a list."""
occurence_count = Counter(List)
return occurence_count.most_common(1)[0][0]
def get_json_values(key,input_data) -> List:
"""return the values of dicts inside a list."""
return [sub_val[key] for sub_val in input_data if key in sub_val]
def max_dict_value(List,key) -> List:
"""return the key that is linked to the most frequent value."""
counts = dict()
for dictionary in List:
counts[dictionary[key]] = counts.get(dictionary[key],0) + 1
max_keys = [key for key, value in counts.items() if value ==
max(counts.values())]
return max_keys
def get_max_dict(List,key) -> List:
"""return the dictionary that maps to the max_dict_value."""
for dictionary in List:
if dictionary[key] == max_dict_value(List,key)[0]:
return dictionary
我的 table 在 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>
{% for element in data.values %}
<tr>
<td>{{ element }}</td>
</tr>
{% endfor %}
</tbody>
</table>
我的问题是数据值只出现在第一列,创建 3 行而不是只出现在第一行。
我该如何解决?这是我 html?
内部的问题您的 HTML table 正在为每个元素创建一个新行 (<tr>
),您想要的是为每个元素创建一个新单元格 (<td>
)每个元素,像这样:
<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>
{% for element in data.values %}
<td>{{ element }}</td>
{% endfor %}
</tr>
</tbody>
</table>
当然,这只会产生一行,这可能是您想要的,但是如果您想要多行,则需要两个循环,一个外部循环用于每个 <tr>
,一个内部循环用于每个 <td>
.