-1

I am new to web development and I am stuck in a process that I need to render the same template with different values(in data shown in code) during 'POST' and 'GET'.

In my terminal, both get and post are invoked by the print statement in the python.

Majority of the code is taken from the 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)

1 Answers1

1

Have the same route handle both methods, then use an if statement to do the correct thing:

@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')
v25
  • 7,096
  • 2
  • 20
  • 36