0

I just want to write a table in HTML in Django, where the data is not from Database. It seems django-tables2 is a good package that I can use in Django. However, my data is not from Database, so maybe it's not necessary to use Django model. Here comes my code of view.py and HTML page:

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 HttpResponse(ret) #return data to HTML

I can use for loop in HTML to display this data but I'm not clearly about how to show them:

    <tbody>
        {% for item in xx %} //I'm not sure
        <tr>
            <td>111</td> //how to display?
        </tr>
        {% endfor %}

Does anyone has any example that I can follow to display the data from view.py in HTML page

0

2 Answers 2

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 %}
Sign up to request clarification or add additional context in comments.

Comments

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)

2 Comments

Hi, Greg, Thanks for your answer, could you explain it more detail ? how to return data to HTML when using pandas?
The value if you print html will give the second snippet. I've included a third snippet that shows how to use this in a Django view to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.