I am running flask python along with HTML, and the problem is simple:
It gives me error 405: Method Not Allowed: The method is not allowed for the requested URL. I have looked this up and people say to include methods=['GET', 'POST'] in the page route, but I already have this.
Python code:
@app.route('/')
@app.route('/home/', methods=['GET', 'POST'])
def home():
global count
guess = request.form.get('guess')
result, valid = guess_word(guess, word, words)
print(result, valid)
try:
guesses[count-1][2] = 'p'
except:
guesses[count-1][2] = ''
if count < 6:
if valid:
guesses[count][0] = guess
guesses[count][1] = result
session['guesses'] = guesses
if valid:
count = 1
return render_template('index.html', guesses=session['guesses'])
HTML code:
<div >
<form method="post">
<input type="text" placeholder="Guess" name="guess">
</form>
</div>
This worked before, and I changed (what I thought was) nothing, but it suddenly stopped working. It gives me the error when I submit the text entry.
CodePudding user response:
Your form has no action
attribute i.e. you have not explicitly said where your data should go to when you submit your form. In the absence of an action
attribute, it will assume your home page which is /
and from your routing, this does not support POST
.
2 possible solutions
- Add the
action
attribute to your form
<form action ="home" method ="post">
- Keep your data as is and add support for POST to your route for
/
i.e. change your routing code to
@app.route('/', methods=['GET', 'POST'])
@app.route('/home/', methods=['GET', 'POST'])