Django render a DynamoDB JSON into a HTML table

175 views
Skip to first unread message

Daniel Pedrajas Pineda

unread,
Apr 18, 2020, 1:23:31 PM4/18/20
to Django users
I'm try to use a Django page as front-end using some AWS DynamoDB tables as back-end. To do so, I use boto3 library and it gets the data from the table correctly but I'm not able to parse the data into a HTML table. I have the following in views.py

def history(request):
     itemsid = list()
     agents = list()
     dates = list()
     source = list()
     dynamodb_resource('dynamodb')
     history_table = dynamodb_resource.Table('name_of_the_table')
     all_items = history_table.scan()
     for p in all_items['Items']:
       itemsid.append((p['id'])),
       agents.append((p['agent'])),
       dates.append((p['date'])),
       source.append((p['source']))
    return render(request, 'history.html', {'itemsid':itemsid, 'agents':agents, 'dates':dates, 'source':source}

The issue is that I don't know how to write the html code to show a table with the rows: id, agent, date and source.

I have the following in history.html


<table>
  {% for i in itemsid %}
  <tr>
    <td>{{ i }}</td>
    ...

but I don't know how to code it (how to loop it) to show the table with the lists as source.

Any idea please about how to parse a Json with the following format into a HTML with Django and Python please?.

JSON from DynamoDB:


{
  'Items: [ {
    'id': '94f'
    'agent': 'aws'
    'date': '04/05
    'source': 'case1'
  }, {
    'id': 'lk42'
      ...
Thank you so much. I'm new in Django and in programming in general so any help is much appreciate.


Daniel Pedrajas Pineda

unread,
Apr 19, 2020, 5:57:03 AM4/19/20
to Django users

     dynamodb_resource('dynamodb')
     history_table = dynamodb_resource.Table('name_of_the_table')
     all_items = history_table.scan()

     return render(request, 'history.html', { items: all_items['Items'] })


Your template:


<
table> {% for item in items %} <tr> <td>{{ item.id }}</td> <td>{{ item.agent }}</td> <td>{{ item.date }}</td> <td>{{ item.source }}</td> </tr> {% endfor %} </table>
Reply all
Reply to author
Forward
0 new messages