Home > Enterprise >  putting dictionary values inside a table in Django template
putting dictionary values inside a table in Django template

Time:04-22

Its my first time using Django template. I'm trying to make a table inside my page with values from a dictionary that I built consuming an API.

Here is how I'm sending the data inside my 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})

Here is how it is my html page:

            <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>

But I'm getting the following error django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '["year_most_launches"]' from 'data["year_most_launches"]'

How can I solve this? Can someone help?

Edit1: tried to use just {{ key }} instead data["key"] but now its not appearing when I render the html

my html page

CodePudding user response:

            <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>

also you can use for loop in template

  • Related