I am facing a problem with flask url routing; it seems routes are not working as expected.
Under project/src/views.py, I have the following sample routes
from flask import (Flask,request,jsonify,Blueprint)
my_view = Blueprint('my_view', __name__)
@my_view.route('/',methods=("GET",))
@my_view.route('/index',methods=("GET",))
def index():
....
<return response code here>
@my_view.route("/key/<inp1>/<inp2>", methods=("POST","GET"))
def getKey(inp1=None, inp2=None):
....
<return response code here>
Now, under project/src/app.py, I have the following code
from ../src.views import my_view
my_app = Flask("myappname")
my_app.register_blueprint(my_view)
my_app.run(debug=True,host=APP_IP,port=APP_PORT)
Now, when I access the URL http://ip:port/index or http://ip:port/key... with valid parameters, it returns 404, with the message "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again." I believe mentioned routes are not working.
The first issue spotted is with your methods parameter. It expects a list/tuple but you're passing a string ('GET'). Change to methods=('GET', ). Note the comma after 'GET' here. Or to avoid potential confusion in the future, use methods=['GET']
The second issue is the way you're import my_view in app.py. Since views.py and app.py are in the same directory, and you're starting your flask app inside that directory, you can just do:
from views import my_view
However you should look into structuring your app as a Python Package
The third issue is a missing from flask import Flask. Maybe you overlooked this when you posted your code.
I tested your code with the above fixes and it works like it should.
EDIT: Thanks to @dirn for pointing out that tuples are accepted for methods parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With