簡體   English   中英

使用 python 中的表格將兩個列表返回到 html 表中

[英]return two lists into html table using tabulate in python

我是 html 的新手,我正在嘗試在 html 頁面上的表格中顯示兩個列表。

我可以顯示列表,但無法將它們顯示為漂亮的表格。

我的代碼是:

from tabulate import tabulate 

def some_function(a, b, c)
    cols = ['Col 1', 'Col 2', 'Col3']
    vals = [a, b, c]
    table = [cols, vals]
    table = tabulate(table, tablefmt='html')
    return table
some_function(1.01, 1.03, 1.05)

這將返回以下 html 代碼:

 <table> <tbody> <tr><td>Col 1</td><td>Col 2</td><td>Col 3</td></tr> <tr><td>1.01 </td><td>1.03 </td><td>1.05 </td></tr> </tbody> </table>

我正在使用 Django 所以我可以使用我的視圖成功地將 html 腳本返回到網站,但它不會將其作為代碼讀取,它顯示為字符串:

看法:

 def post(self, request):
        form_c = CalculatorForm(request.POST, prefix='form_c')
        try:
            if form_c.is_valid():
                post = form_c.cleaned_data
                numbers = some_function(1.01, 1.03, 1.05)

        except:
            pass

        args = {
            'form_c': form_c, 'form_cols': numbers,
        }
        return render(request, self.template, args)

我的 html:

    <div class="container">
        {{ form_cols }} # <-- where I want my table
    </div>

在 html 中寫入(假設默認模板引擎):

    <div class="container">
        {{ form_cols|safe }}
    </div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM