Sunday, April 19, 2020

Re: Django render a DynamoDB JSON into a HTML table

Here the solution: https://stackoverflow.com/questions/61285947/django-render-a-dynamodb-json-into-a-html-table

Thank you to https://stackoverflow.com/users/6460856/swetank-poddar

View.py:


def
history(request): 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>

El sábado, 18 de abril de 2020, 19:23:31 (UTC+2), Daniel Pedrajas Pineda escribió:
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.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a301efc4-5c67-402d-8986-59faddbc482d%40googlegroups.com.

No comments:

Post a Comment