1
You don’t need to return Django objects to create templates, you can use any data. The render() function allows you to combine context with the regular HttpResponse. You pass it the request which was given to the view calling it, the name of the template you want to render, and then a dictionary of data to provide to the template.
def device_manager_submit(request):
'''Switch manager page'''
ret = rest.send_device_tor(device_name) #data from rest API exist in the form of array of dictronary: [{}, {}, {}]
return render(request, 'some_template.html', {'devices': ret}) #return data to HTML
Assuming that ret
contains some objects with a name
and description
, we can loop through devices
like so:
<tbody>
{% for device in devices %}
<tr>
<td>{{ device.name }}</td>
<td>{{ device.description }}</td>
</tr>
{% endfor %}
0
One way would be to use pandas to load the data, and then use the DataFrame.to_html()
to output the data into an html table. See the example below:
import pandas as pd
data = [{'column1': 1, 'column2': 2}]
df = pd.DataFrame(data)
html = df.to_html()
Html will result in:
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>column1</th>
<th>column2</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>2</td>
</tr>
</tbody>
</table>
In a Django view this would be:
@api_view(['GET'])
def showData(request):
data = [{'column1': 1, 'column2': 2}]
df = pd.DataFrame(data)
html = df.to_html()
return HttpResponse(html)
- Generate a unique key after refresh Django Admin form
- What is the best way to use elasticsearch in Django Restframework
- How to captalize letter on django rest serialize
- Django Mezzanine CMS base urls.py location?
- Are these VirtualHost settings workable with Django?
Source:stackexchange.com