I am going through the Flask quickstart guide, but the examples of routing with variables do not work for me.
from flask import Flask
app = Flask(__name__)
@app.route('/') # fine
def index():
return 'Index Page'
@app.route('/hello') # fine
def hello():
return 'Hello World!'
@app.route('/user/<username>') # <- fails unless trailing slash here
def show_user_profile(username):
return 'User {}'.format(username)
if __name__ == '__main__':
app.run()
- http://127.0.0.1:5000/user/bob redirects to http://127.0.0.1:5000/user/bob/ and gives me a 404
- http://127.0.0.1:5000/user/bob? does not redirect and the page renders correctly
- If I change the code to
@app.route('/user/<username>/')
, http://127.0.0.1:5000/user/bob redirects to http://127.0.0.1:5000/user/bob/ and the page renders correctly
I have also tried the code exactly as in the guide. Is the error in my first bullet point expected? Is code in the quickstart version supposed to work? Or have I misunderstood something?
I'm using Python 2.7.10, Flask 0.10.1, Werkzeug 0.10.4
I tried out this example in python2 and it works fine, but in python3 problem you described occurs. Are you using 3rd version? And if you do are you sure you really need it?
Look at this http://flask.pocoo.org/docs/0.10/python3/#python3-support