I'm trying to make a table in Django template. I'm sending a dict as my data inside my views.py
like this:
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})
My 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
My table in HTML code:
<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>
My problem is that the values of data just appears in the first columm, creating 3 rows instead of just appearing in the first row.
How can I solve that? It is a problem inside my html?
CodePudding user response:
Your HTML table is creating a new row (<tr>
) for each element, what you want is to create a new cell, (<td>
) for each each element, like this:
<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>
Of course, this will only produce ONE row, which may be what you want, but if you wanted several rows you would need two loops, an outer one for each <tr>
, and an inner one for each <td>
.