簡體   English   中英

flask python 中不呈現不同的模板

[英]different templates are not rendering in flask python

我是 web 開發的新手,我被困在一個過程中,我需要在“POST”和“GET”期間用不同的值(在代碼中顯示的數據中)呈現相同的模板。

在我的終端中,get 和 post 都是由 python 中的打印語句調用的。

大部分代碼取自Record voice with recorder.js 並將其上傳到 python-flask 服務器,但 WAV 文件已損壞

@app.route("/", methods=['GET'])
def index_GET():
    print('data in get',data)
    print('GET IS INVOKED')
    return render_template("index.html",q = "hello world",data = 8)


@app.route("/", methods=['POST'])
def index_POST():
    f = request.files['audio_data']
    basepath = os.path.dirname(__file__)
    x = str(datetime.datetime.now()).replace(" ", "").replace(":","").replace(".","")+'.wav'
    #upload to database folder uploads
    with open(basepath+'/uploads/'+x, 'wb') as audio:
        f.save(audio)
    
    print("POST IS INVOKED")
    print(data)
    print('-'.center(100,'-'))
    return render_template("index.html",q = "hello world",data = 1000)

讓相同的路由處理這兩種方法,然后使用if語句來做正確的事情:

@app.route("/", methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # Do some processing
        return render_template('index.html', data = 'some value')
    elif request.method == 'GET':
        return render_template('index.html', data = 'some other value')

暫無
暫無

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

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