簡體   English   中英

使用render_template()中的'for'循環在html中顯示表

[英]displaying table in html using 'for' loop from render_template()

使用Python Flask應用程序使用'for'循環實現呈現html表。 但是數據不會呈現空白<tr><tr>

我通過從render_template傳遞dff.to_html()並在'for'循環中檢索html中的表來顯示,從而將數據幀轉換為html。

Python代碼:

return render_template('index.html', table = dff.head(50).to_html())

HTML代碼:

         <table>
            <tbody>

            {% for row in table %}
                <tr>
                    <td>{{row.name}}</td>
                    <td>{{row.link_to_detail}}</td>
                    <td>{{row.link}}</td>
                    <td><input name ="get poster"  type="submit" value={{row['id']}}></td>
                </tr>
            {% endfor %}
            </tbody>
        </table>

實際結果是

<tr>
      <td></td>
      <td></td>
      <td></td>
      <td><input name="get poster" type="submit" value=""></td>
</tr>

預期結果 :

    <tr>
      <td>name</td>
      <td>link_to_detail</td>
      <td>link</td>
      <td><input name="get poster" type="submit" value=""></td>
    </tr>

“””

dff.head(50).to_html()返回一個html表,而不是一個python對象。

這個html表實際上是一個字符串,你正在迭代字符( row ),它沒有任何名為namelinklink_to_detail屬性......

如果您發布dff數據幀的樣本,我將能夠完成此答案。

我從來沒有工作python,但想法是你需要發送變量來查看。

return render_template('index.html', table = dff.head(50).to_html(),  name='name' ,row.link_to_detail= 'row.link_to_detail' , link ='link')

它可以是這樣的

不要使用dff.head().to_html()返回HTML表作為字符串,而是dff[:50].values (給出前50行所有列的2d數組)然后row.0row.1獲取每行row.1兩列的值。

暫無
暫無

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

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