-1

Hello i am doing a proyect in pyhton with flask an i pretend to introduce a txt and read it.

This code is part of home.html:

<input type="file" id="gameTXT" name="gameTXT" accept="txt">
<input type="submit" id="submitTXT" value="Submit">

and this one is the part of python:

@app.route("/")
def home():
    return render_template('home.html')

How can i get the file? I read that i need to put methods=['GET'] but i don't know where to put it

I try to put methods=['GET'] in the app.route("/") but it doesn't work and it's understandable. I expect to get the file

Pepe Diaz
  • 1
  • 2

2 Answers2

1

if you want to get the file, you need to first know what is your form method, as you mentioned GET, I assume GET. (But I insert POST just in case).

You need to create the function, mentioning that the method is GET, and then you need to get the data that was send, which is request.form['name']

Get the data received in a Flask request Referring above, your code should be

@app.route("/", methods=['GET', 'POST'])
def home():
    data = request.form['gameTXT']
    ### do whatever you want with data
    return render_template('home.html')
Joshua
  • 144
  • 7
  • with this code returns me BadRequestKeyError,only changing methods and the data line – Pepe Diaz Oct 28 '22 at 08:19
  • Hi Pepe, please try to change to this data = request.form['gameTXT'] to data = request.form.get('gameTXT') Referring to https://stackoverflow.com/questions/52596200/badrequestkeyerror – Joshua Oct 28 '22 at 08:24
-2

Try adding method in the route() like this :

@app.route("/", methods=["GET"])
def home():
    return render_template('home.html')
sachin
  • 1,075
  • 1
  • 7
  • 11