当隐藏引用与函数关联时,不会调用该函数

2024-10-04 05:24:45 发布

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

我挠头好几天了,因为我不明白为什么在添加隐藏引用时单击“刷新统计信息”时,函数crypto_insert()没有被调用

应用程序声明如下:

init.py

from tracker.crypto.crypto_portfolio import crypto_portfolio
app.register_blueprint(crypto_portfolio)

蓝图路线在此声明:

core.py

@core.route('/crypto_insert', methods=['GET','POST'])
def crypto_insert():
    return render_template('crypto_portfolio.html')

python/flask部分位于此处:

加密组合.py

# Coingecko API library
from pycoingecko import CoinGeckoAPI

crypto_portfolio = Blueprint('crypto_portfolio',__name__)
print('******************HELLOO1111**************')

@crypto_portfolio.route('/crypto_insert', methods=['GET','POST'])
@login_required
def crypto_insert():
    print('******************HELLOO2222**************')
    if request.form.get("ident") == "formCrypto":
        print('******************HELLO3333**************')

        crypto_token = 'bitcoin'
        crypto_currency = 'usd'
        print('******************HELLOO4444**************')
        print(crypto_token)

        return render_template('crypto_portfolio.html',
                                formCrypto=form,
                                crypto_token=crypto_token,
                                crypto_currency=crypto_currency,
                                )

用于呈现值的烧瓶/引导部分如下所示:

crypto_portfolio.html

{% extends "base.html" %}
{% block content %}

<!-- Container for the whole page -->
  <div class="container">
    <div class="row">
      <div class="col-md-12">
        <div class="bg-light p-3">

<!-- Flash messages section -->
          <div class="forms">
            <div class="formCrypto">
              <form method="post" action="{{ url_for('core.crypto_insert') }}">

                      <div class="alert alert-success alert-dismissable" role="alert">
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close">
                        </button>
                      </div>

<!-- Portfolio Summary and refresh button section -->
                <h2><b>Portfolio</b> Summary:
                  <button type="submit" class="btn btn btn-primary float-end" data-bs-toggle="modal" data-bs-target="#modalrefresh">Refresh Stats</button> </h2>
                  <input type=hidden name="ident" value="formCrypto">
              </form>
            </div>
          </div>

<!-- Portfolio Summary section -->
          <div class="container">
            <div class="row align-items-start">
              <div class="col">
                <b>Total Invested: </b>${{crypto_token}}
              </div>
              <div class="col">
                <b>Total Earnings: </b>${{crypto_currency}}
              </div>

            </div>
          </div>


        </div>
      </div>
    </div>
  </div>

  {% endblock %}

但在调试终端中,仅打印hello1:

****HELLOO1111

我在这里错过了什么


Tags: pydivformtokenhtmlbuttonalertcrypto
1条回答
网友
1楼 · 发布于 2024-10-04 05:24:45

似乎您正在为/crypto_insert定义两条路由(在core.pycrypto_portfolio.py);并且只调用crypto_portfolio.html中形式的core.crypto_insert

为了更好地理解,我建议使用以下内容更新crypto_portfolio.py

# Coingecko API library
from pycoingecko import CoinGeckoAPI

crypto_portfolio = Blueprint('crypto_portfolio',__name__)
print('******************HELLOO1111**************')

@crypto_portfolio.route('/crypto-insert-with-form-handling', methods=['GET','POST'])
@login_required
def crypto_insert_with_form_handling():
    print('******************HELLOO2222**************')
    if request.form.get("ident") == "formCrypto":
        print('******************HELLO3333**************')

        crypto_token = 'bitcoin'
        crypto_currency = 'usd'
        print('******************HELLOO4444**************')
        print(crypto_token)

        return render_template('crypto_portfolio.html',
                                formCrypto=form,
                                crypto_token=crypto_token,
                                crypto_currency=crypto_currency,
                                )
    
    return render_template('crypto_portfolio.html')

要在同一url上有POST请求,请更新行crypto_portfolio.html

<form method="post" action="{{ url_for('core.crypto_insert') }}"> 

<form method="post" action="#"> 

您现在可以尝试/crypto-insert-with-form-handling/crypto_insert

相关问题 更多 >