sqlalchemy序列化的json输出不会呈现在html表中

cvxl0en2  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(193)

我将此python代码添加到我的flask中以刷新我的sqlalchemy数据库表,而不刷新整个页面,但我需要序列化json输出,因此我还补充了代码:

@portfolioPath.route('/wallet_refresh', methods=['GET'])
def wallet_refresh():
    if request.method == "GET":
        data_query = HoldingsTable.query.all()

        print('\n--- Encode Object into JSON formatted Data using jsonpickle ---')
        TableJSON = jsonpickle.encode(data_query, unpicklable=False)
        print(TableJSON)

        print('\n--- Writing JSON Encode data into Python String ---')
        TableJSONData = json.dumps(TableJSON, indent=4)
        print(TableJSONData)

        print('\n--- Decode JSON formatted Data using jsonpickle ---')
        TableJSON = jsonpickle.decode(TableJSONData)
        print(HoldingsTable)

        # Let's load it using the load method to check if we can decode it or not.
        print('\n--- Load JSON using loads() method ---')
        TableJSON = json.loads(TableJSON)
        print(TableJSON)

        return json.dumps({"data":TableJSON})
    return render_template("portfolio.html")

我添加了处理按钮执行的jquery:

<script type=text/javascript>
$('#wallet_refresh_id').bind('click', function() {
  $.getJSON('/wallet_refresh', function(all_crypto_data) {
     for (var i of TableJSON.data ){ // using .data, we will return json in flask 
     $("#data_holder").append(
       `
       <td>${i.crypto_id}</td>
       <td>${i.crypto_date_purchase}</td>
       <td>${i.crypto_name}</td>
       <td>${i.crypto_ticker}</td>
       <td>${i.crypto_shares}</td>
       <td>${i.crypto_price_purchase}</td>  
       <td>${i.crypto_cost_total}</td>      
       `       
       )
     } 
  });
});
</script>

js刷新按钮位于此处:

<h3>REFRESH THE DATABASE</h3>
  <button id="wallet_refresh_id" class='btn btn-default'>Refresh</button>

重新加载整个页面的 flask 刷新按钮工作正常:

<form action="{{url_for('core.crypto_portfolio')}}" method="POST">
  <div class="form-group">
    <div class="col-md-2 col align-self-end">
          <button class="btn btn-primary float-end" type="submit" style="margin:5px;">1. Find It!</button>             
    </div>
  </div>   
</form>

在投资组合表中:

<!-- Equity Holdings Overview Table -->
<table class="table table-hover table-striped table-sm">
  <tr>
    <th>ID</th>
    <th>Trade Date</th>
    <th>Name</th>
    <th>Symbol</th>
    <th>Shares</th>
    <th>Purchase Price</th>
    <th>Total Cost</th>
    <th style="text-align:right">Action</th>
  </tr>
  {% for row in all_crypto_data_html %}
  <tr id="data_holder">
    <td>{{row.crypto_id}}</td>
    <td>{{row.crypto_date_purchase}}</td>      
    <td>{{row.crypto_name}}</td>
    <td>{{row.crypto_ticker}}</td>
    <td>{{row.crypto_shares}}</td>
    <td> &#x24; {{row.crypto_price_purchase}}</td>
    <td> &#x24; {{row.crypto_cost_total}}</td>
  </tr>

后端输出在终端中呈现:
---使用loads()方法加载json---[{'sa_instance_state':{'instance':无,'committed_state':{},'key':[{'py/type':'adisely.models.models.cryptotable'}[1],无],'load_options':[],'class':{'py/type':'adisely.models.models.cryptotable'},'expired"属性:[],'load_path':[{'py/type':'adisely.models.models.cryptotable'},无]],“经理”:{'class_uu':{'py/type':'advisedly.models.models.cryptotable'},'crypto_udate_upurchase':'2021-06-18','crypto_uticker':'mp','crypto_ucost_utotal':'151.722','crypto_o_o_udate price_ucurrent':'0','crypto_o_o_o总收益':'0','crypto_o_o_o股份':'0','0','crypto_u股份':'0.151722','crypto_u名称':'mantra dao.',“crypto_price_current':“0”,“crypto_price_gain':“0'”,{Sau示例_state':{'instance':无,'committed_state':{},'key':[{'py/type':'advisely.models.models.cryptotable'},[2],无],'load_options':[],'class':{'py/type':'advisely.models.models.cryptotable','expiredƗ属性:[],'加载路径':{'py/type':'advisedly.models.models.cryptotable'},无,'manager':{'class':{'py/type':'advisedly.models.cryptotable'},'加密日期购买':'2021-06-22','加密股票':'','加密成本总额':'0','加密日期价格当前':'0','加密总收益':'0','加密股票':'349比特币名称:',“crypto_id”:2,“crypto_price_purchase”:“0”,“crypto_price_current”:“0”,“crypto_price_gain”:“0”;{sa_instance_state”:{instance”:无,“committed_state”:{},'key':[{'py/type':'advisely.models.cryptotable',[3],无],'load_options':[],'class':{'py/type':'advisely.models.models.cryptotable','expired",',“加载路径”:[{'py/type':'advisedly.models.models.cryptotable'},无]],'manager':{'class':{'py/type':'advisedly.models.cryptotable'}},'cryptou date_purchase':'2021-06-22','cryptou股票代码':'','cryptou成本总额':'2','cryptou日期(价格)当前':'4','cryptou总收益':'6','cryptou股票':'1972.67,,‘加密名称’:‘以太坊’,‘加密id’:3,‘加密价格购买’:‘1’,‘加密价格当前’:‘3’,‘加密价格增益’:‘5’,{sa_示例状态’:{'instance':无,'committed_状态':{},'key':[{'py/type':'advisedly.models.cryptotable',[4],无],'load_options':[],'class':{'py/type':'advisedly.models.cryptotable',“过期属性”:[],“加载路径”:[{'py/type':'advisedly.models.models.cryptotable',none],“管理者”:{'class'py/type':'advisedly.models.models.cryptotable'}},'crypto'date'purchase':'2021-06-22','crypto'ticker':'','crypto'cost'total':'0.198239','crypto'date price'u current':'4','crypto',‘加密股票’:‘0.198239’,‘加密名称’:‘dogecoin’,‘加密id’:4,‘加密价格购买’:‘1’,‘加密价格当前’:‘3’,‘加密价格收益’:‘5’}]
终端日志代码为:
2021-07-11 23:25:44561信息sqlalchemy.engine.engine回滚
127.0.0.1--[11/jul/2021 23:25:44]“获取/钱包刷新http/1.1”200-
但我的问题是,表格不会刷新,整个页面会重新加载。。。所以很明显,这里发生了一个异常,移动到页面重新加载,而不是仅重新加载表。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题