I am closely following the very brief tutorial from Flask-wtf here. I have an issue where after I submit my name in my submit page form, it gives a "405 Method Not Allowed" message instead of directing me to the success page.
from flask import Flask, render_template, redirect
from forms import MyForm
app = Flask(__name__)
app.secret_key = 'mysecretKey'
@app.route('/submit', methods=('GET', 'POST'))
def submit():
form = MyForm()
if form.validate_on_submit():
return redirect('/success')
return render_template('submit.html', form=form)
@app.route('/success')
def success():
return "Well done for entering your name!"
if __name__ == '__main__':
app.run(debug=True)
My form is here:
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import DataRequired
class MyForm(FlaskForm):
name = StringField('name', validators=[DataRequired()])
My submit.html code is shown below (just like in the tutorial):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Password page</title>
</head>
<body>
<form method="POST" action="/">
{{ form.hidden_tag() }}
{{ form.name.label }} {{ form.name(size=20) }}
<input type="submit" value="Go">
</form>
</body>
</html>
EDIT: The if form.validate_on_submit() condition does not return True so the contents of the loop do not execute. I added a simple print statement in it which didn't execute.
action="/"needs to be changed toaction="/submit"@app.route('/success')doesn't specify the access methods, so it'll default to justGET. You need@app.route('/success', methods=['POST'])action="/"toaction="{{ url_for('submit') }}". I'm missing something basic here, sorry