1

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.

11
  • 2
    maybe action="/" needs to be changed to action="/submit" Commented Dec 7, 2019 at 11:37
  • Sure. @app.route('/success') doesn't specify the access methods, so it'll default to just GET. You need @app.route('/success', methods=['POST']) Commented Dec 7, 2019 at 11:37
  • @roganjosh I still get the same error after adding this. Commented Dec 7, 2019 at 11:45
  • Hmm, ok. What route is throwing the error (look into the console to see where the request went)? Something seems awry Commented Dec 7, 2019 at 11:47
  • 1
    Try changing action="/" to action="{{ url_for('submit') }}". I'm missing something basic here, sorry Commented Dec 7, 2019 at 11:52

2 Answers 2

3

I'd be lying if I pretended to know exactly how all relative paths are resolved. However, you can fix this by changing:

<form method="POST" action="/">

to:

<form method="POST" action="{{ url_for('submit') }}">

It's one of those things that it really does make sense to pass the issue off to the library to figure out. Use url_for which also works in Jinja2 when rendering the template.

Sign up to request clarification or add additional context in comments.

1 Comment

@piccolo it would, in this case. Wait till you have blueprints etc. Then it's a bit much. Just point to the view name and avoid the stress :)
0

The problem is you are not mentioning that the method success should handle POST requests.

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)

with

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)

# This is the change I made.
@app.route('/success', methods=['POST'])
def success():
    return "Well done for entering your name!"


if __name__ == '__main__':
    app.run(debug=True)

3 Comments

Hi Sanil, I still get the same error after adding the POST method to success.
@Sanil why would you think this was the problem? The form handling code is in submit, which already accepts POST, not success.
@DanielRoseman it looks like the if form.validate_on_submit() does not return True and so it can't go to the success page. What are your thoughts?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.