【问题标题】:different templates are not rendering in flask python不同的模板没有在烧瓶python中呈现
【发布时间】:2020-08-22 10:57:08
【问题描述】:

我是 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)

【问题讨论】:

    标签: javascript python flask audio backend


    【解决方案1】:

    用相同的路由处理这两种方法,然后使用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')
    

    【讨论】:

      猜你喜欢
      • 2016-05-14
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      相关资源
      最近更新 更多