gpt4 book ai didi

javascript - Flask Python中未呈现不同的模板

转载 作者:行者123 更新时间:2023-12-03 02:30:08 24 4
gpt4 key购买 nike

我是Web开发的新手,因此我陷入了一个过程,即需要在“POST”和“GET”期间使用具有不同值(代码中显示的数据)的相同模板进行渲染。
在我的终端中,get和post都由python中的print语句调用。
大部分代码取自Record voice with recorder.js and upload it to python-flask server, but WAV file is broken

@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')

关于javascript - Flask Python中未呈现不同的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63535354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com