keddad
keddad

Reputation: 1816

Flask returns 404 even though route is clearly defined

I've got some function using a blueprint:

@election_blueprint.route("/getlast/{string:type}")
def get_specific_last(election_type: str):
    some code here

And then I register this blueprint before starting an app:

app.register_blueprint(election_blueprint, url_prefix="/election")

And, after that, Flask says that this method is defined in routes:

# FLASK_APP='owo/app.py' flask routes
Endpoint                     Methods  Rule
---------------------------  -------  ----------------------------------------------------------
elections.get_elections      GET      /election/find/{string: type}
elections.get_last           GET      /election/getlast/
elections.get_specific_last  GET      /election/getlast/{string:type} <-- There it is!

But when I try to get it from client, I get 404, even though other methods, even declared in this blueprint, appear to work fine. What am I doing wrong?

For example, if I just go to

http://localhost/election/getlast/sometype

It returns 404, but if I use another method, like

http://localhost/election/getlast/

It works fine.

Upvotes: 1

Views: 601

Answers (1)

Kenny Aires
Kenny Aires

Reputation: 1438

I believe there's a typo the way it is written on your code using "{" instead of "<". Also, the variable name on the route should match variable name on the function:

@election_blueprint.route("/getlast/<string:election_type>")
def get_specific_last(election_type: str):
    some code here

good ref on: https://hackersandslackers.com/flask-routes/ Hope it suits you well :)

Upvotes: 1

Related Questions