使用DataTable和Flask从postgresql呈现表

2024-04-25 22:43:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在网页中显示来自PGsql的表,我正在使用jQuery DataTables进行此配置(https://datatables.net/extensions/scroller/examples/initialisation/large_js_source.html

我创建了一个python类app.py,以从我的表srccommercialedata创建属性:

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://' +....

class srccommercialedata(db.Model):

   id_technique =  db.Column(db.Integer,primary_key = True)
   tit_nom_rso = db.Column(db.Text) 
   tit_nom_usage =  db.Column(db.Text) # Float



   def __init__(self,id_technique,tit_nom_rso,tit_nom_usage):
       self.id_technique = id_technique
       self.tit_nom_rso = tit_nom_rso
       self.tit_nom_usage = tit_nom_usage        


@app.route('/')
def home() :
rows = srccommercialedata.query.all()
return render_template('home.html', rows=rows)

if __name__ == "__main__" :
    app.run()

这是我的home.html代码:

<table id="example" class="display nowrap" style="width:100%">
    <thead>
      <tr class='header'>
        <th> id_technique </th>          
        <th>tit_nom_rso </th>
        <th>tit_nom_usage </th>

      </tr>
    </thead>

    <tbody>
      {% for row in rows %}
      <tr>
        <td>{{row.id_technique }}<td>
        <td>{{row.tit_nom_rso }}</td>
        <td>{{row.tit_nom_usage }}</td>


      </tr>

      {% endfor %}

    </tbody>

</table>

<script>
(document).ready(function() {
$('#example').DataTable( {
    serverSide: true,
    ordering: false,
    searching: false,
    ajax: function ( data, callback, settings ) {

        setTimeout( function () {
            callback( {
                recordsTotal: 5000000,
                recordsFiltered: 5000000
            } );
        }, 50 );
    },
    scrollY: 200,
    scroller: {
        loadingIndicator: true
    },
} );
} );

</script>
</body>
</html>

我的表不是作为示例呈现的,而是作为HMTL中的简单文本呈现的,如何修改代码以获得正确的结果来显示数据

多谢各位


Tags: selfidappdbhtmlusagenomtr