第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何讓 Flask 在 HTML 中運(yùn)行并在同一頁(yè)面打印結(jié)果?

如何讓 Flask 在 HTML 中運(yùn)行并在同一頁(yè)面打印結(jié)果?

慕慕森 2023-06-20 16:35:44
我一直在嘗試使用我之前編寫(xiě)的腳本將阿拉伯?dāng)?shù)字轉(zhuǎn)換為羅馬數(shù)字。用戶(hù)輸入數(shù)字,我的腳本將它們轉(zhuǎn)換為羅馬數(shù)字。腳本運(yùn)行良好,但我嘗試將其嵌入網(wǎng)頁(yè)時(shí)無(wú)法正常工作。我用谷歌搜索了解決方案,每個(gè)人都告訴我需要從表單中獲取值,我這樣做了:                <form action="toroman" method="POST">                    <input type="number" name="arabic" onChange="toroman(this.value)" oninput="toroman(this.value)">                    <p>{{ romanfinal }}</p>                </form>這是我的 server.py,它應(yīng)該打印出同一頁(yè)的編號(hào),但它并沒(méi)有這樣做,而是當(dāng)我在提交值時(shí)按“Enter”時(shí)它會(huì)創(chuàng)建一個(gè)新頁(yè)面并顯示正確答案。from flask import Flask, render_template, requestapp = Flask(__name__)@app.route("/")def index():    return render_template("index.html")@app.route("/toroman", methods=['POST'])def toroman():    arabic = request.form['arabic']    # some unnecessarily numerous lines of code that basically turn Arabic-system number to Roman system    return romanfinalif __name__ == "__main__":    app.run(debug=True)這就像它唯一一次真正起作用,當(dāng)我嘗試將其更改為其他內(nèi)容時(shí),它只會(huì)給我錯(cuò)誤。請(qǐng)告訴我我到底不明白什么。
查看完整描述

2 回答

?
喵喵時(shí)光機(jī)

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊

你的toroman():函數(shù)應(yīng)該返回帶有參數(shù)的 index.html :


@app.route("/toroman", methods=['POST'])

def toroman():

    arabic = request.form['arabic']

    # some unnecessarily numerous lines of code that basically turn Arabic-system number to Roman system

    return render_template("index.html", data = romanfinal)

data然后你可以像這樣在你的 HTML 頂層使用這個(gè)值:{{data}}


查看完整回答
反對(duì) 回復(fù) 2023-06-20
?
qq_遁去的一_1

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊

燒瓶


from flask import Flask, render_template, request, jsonify


app = Flask(__name__)


@app.route("/")

    def index():

return render_template("index.html")


@app.route("/toroman", methods=['POST'])

def toroman():

    arabic = request.data['arabic']

    #pass arabic into your translation function

    translation = translate()

    #return JSON response to AJAX

    return jsonify(translation = translation)

if __name__ == "__main__":

    app.run(debug=True)

JS


$(document).ready(function(){

  document.getElementById('toroman_form').addEventListener('keyup', function() {

   $.ajax({

        type: 'POST',

        url: '/toroman', //flask route to which data is sent

        data: $('#arabic').val(), // input field value

        contentType:'application/json; charset=utf-8',

        dataType: "json",

        success: function(data) {

            translation = data.translation //response received from flask

            $('#translated').text(translation) //display translation in html <p> tag

        },

        error: function() {

            alert("Transaction Failed");

      }

  });

});

}

HTML


<form id="toroman_form">

    <input type="number" id="arabic">

    <p id="translated"><!--translation is dislayed here--></p>

</form>


查看完整回答
反對(duì) 回復(fù) 2023-06-20
  • 2 回答
  • 0 關(guān)注
  • 229 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)