I know that this question has been asked a few times but none of the answers seem to solve my issue. I’m trying grab some input from my html page and use it in my python/flask file. When I print out the request.form.get() variable to try and figure out what was happening it returns None (which is not what I want). I’ve tried using request.form[], request.value.get(), request.args.get(), using a XMLHttpRequest in a js file to send the data to the python file and the result was the same as when I used request.form.get() with each. I’ve also tried using separate views to receive the form data. When I tried request.form[] in my flask file and tried to make this a post request I got a "POST /truth2 HTTP/1.1" 400 -
. One post said that this might be thanks to an incorrect key being used for the request object but if that’s what’s going on I’m not sure how to correct it. Any help with this is greatly appreciated and thank you for thanking the time to read this.
HTML
<div class="P2truth_container">
<form method="post" action="/">
<input id="truth2" name="Player2T"></input>
<button class="sub_btn2">Liar</button>
</form>
</div>
<div>
<form>
Python
@app.route('/truth2', methods=['POST', 'GET'])
def truth2Get():
if request.method == 'POST':
P2truth = request.form.get('Player2T')
print('fixed', P2truth)
storedP2 = session['Player2T'] = P2truth
print(storedP2)
return render_template('index.html', P2truth=P2truth)
Other view in Python
@app.route('/P1Liar', methods=['POST', 'GET'])
def P1Liar():
if request.method == 'POST':
truth2 = session.get('Player2T')
print(truth2)
elif request.method == 'GET':
truth2 = request.form.get('Player2T')
print(truth2)
Next_card = session.get('New_card')
print(Next_card)
# truth2 = session.get('truth2')
truth3 = request.form.get('name', 'truth3')
truth4 = request.form.get('name', 'truth4')
try:
print("truth2: ", truth2)
if truth2 != None:
print("truth2: ", truth2)
if truth2 != Next_card:
print("NC: ", Next_card)
print("T2: ", truth2, "Next_card: ", Next_card)
secondhand.extend(pile)
print("P2: ", secondhand)
print("P1: ", firsthand)
print("P3: ", thirdhand)
print("P4: ", fourthhand)
pile.clear()
print(pile)
return render_template('index.html', secondhand=secondhand)
else:
firsthand.extend(pile)
pile.clear()
print(pile)
print("P1: ", firsthand)
print("P2: ", secondhand)
print("P3: ", thirdhand)
print("P4: ", fourthhand)
return render_template('index.html',firsthand=firsthand)
if truth3 != None:
if truth3 != Next_card:
print(Next_card)
thirdhand.extend(pile)
print("P2: ", secondhand)
print("P1: ", firsthand)
print(pile)
return render_template('index.html', pile=pile)
else:
firsthand.extend(pile)
print(pile)
print("P1: ", firsthand)
print("P2: ", secondhand)
return render_template('index.html', pile=pile)
if truth4 != None:
if truth4 != Next_card:
print(Next_card)
thirdhand.extend(pile)
print("P2: ", secondhand)
print("P1: ", firsthand)
print(pile)
return render_template('index.html')
else:
firsthand.extend(pile)
print(pile)
print("P1: ", firsthand)
print("P2: ", secondhand)
return render_template('index.html')
except ValueError:
return ("nothing")
Javascript
const P2Liar = document.getElementById("truth2");
const P2Liarbtn = document.querySelector(".sub_btn2")
P2Liarbtn.addEventListener("click", function(e) {
e.preventDefault()
console.log(P2Liar.value);
const xhr = new XMLHttpRequest();
const method = 'POST'
const url = '/truth2'
xhr.open(method, url)
xhr.send()
});