Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask Basics Character Builder Forms

Daniel Deiana
Daniel Deiana
11,341 Points

Method Not Allowed

Good day!

I'm getting this message when trying to run the code supplied in this video.

"Method Not Allowed The method is not allowed for the requested URL"

--app.py-- @app.route("/save", methods=["POST"]) def save(): return redirect(url_for("index"))

--index.html-- <form action="{{ url_for('save') }}" method="POST">

What am i doing wrong?

kindest regards Dan

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,722 Points

I am not sure if this answers your question. But, when I get that error it typically means I neglected to handle the GET method (rendering of the form) on the @app.route decorator. To see if this is the case, you could add in a tiny bit of code to print to the console if save() is called with a GET.

from flask import request

@app.route('/save', methods=['GET', 'POST'])
def save():
    if request.method == 'POST':
        clean_and_validate_the_data() # some functional code you need to handle POST data
        save_the_data() # some functional code to save our data 
    else:
        print "DEBUG: save() GET method was called"  # some message which echos to the console or throw a custom error
    return redirect(url_for("index"))
Daniel Deiana
Daniel Deiana
11,341 Points

Thanks for your answer Jeff, that worked!!

While I agree that the method above works, can someone provide more of an explanation as to why this is needed? My understanding is that the form is calling the GET method before the POST method which would return "Method Not Allowed" because GET wasn't listed as a method in the @app.route decorator. Why was this not the case in this lesson's video? The example provided by Kenneth Love does not handle the GET method and yet does not cause any errors. For reference, I'm developing locally, not using workspaces. Thanks!

Jeff Muday
Jeff Muday
Treehouse Moderator 28,722 Points

That is an astute observation--

As you know, there are various methods which can be directed by a user-- GET, POST, PUT, DELETE, and PATCH. Most students are only working with GET and POST in their nascent projects and learn to use PUT, DELETE, and PATCH when they are learning API design patterns The design pattern Kenneth Love is demonstrating is a that of a single-purpose "view" accepting only POST requests-- this is to reinforce the concept of requirement to specify the method in Flask's route decorator and to help the student understand how the POST method data is transmitted differently than GET data.

Kenneth will also introduce a design pattern of views that will handle GET request (to render a form) and POST request (data validation, handling) in later lessons as this is a pretty typical design pattern a student or professional would encounter on a daily basis.

Kenneth's code as shown in lecture works for me and I'm also developing locally. However, I do get the error when I type this in the browser:

http://0.0.0.0:8000/save

which is trying to turn a 'post' to a 'get' when /save is only supposed to be a post.

So I just enter

http://0.0.0.0:8000

and fill in the form and click "Let's Build it!" and that works as expected