I'm just started to play with Flask, so in all likelihood this is a seriously noobish question. This app is running on Google App Engine SDK 1.7.4. Flask 0.9, Werkzeug 0.9 and Jinja2 2.6.
The following code works as expected:
from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route('/')
def hello():
return "Main page"
@app.route('/hello/', methods=['GET', 'POST'])
@app.route('/hello/<name>', methods=['GET', 'POST'])
def hello(name=None):
return render_template('hello.html', name=name)
if __name__ == "__main__":
app.run()
However, if I reverse the route handlers, going to the /hello/ renders as if I went to /
from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route('/hello/', methods=['GET', 'POST'])
@app.route('/hello/<name>', methods=['GET', 'POST'])
def hello(name=None):
return render_template('hello.html', name=name)
@app.route('/')
def hello():
return "Main page"
if __name__ == "__main__":
app.run()
Worse yet, going to /hello/, for example /hello/John, results in error 500.
Is this normal behavior and the order of routes matters? If so, please also point me to relevant docs and, if possible, provide an explanation of why this order is so important.
You're creating two functions with the same name (hello
). Rename the second one:
@app.route('/')
def index():
return "Main page"