为python应用程序定义不同的路由

时间:2013-01-15 09:48:48

标签: python routes flask

这是我的python代码

    import os
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello World!'
def Fun():
    return 'Having Fun!'

if __name__ == '__main__':
    # Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

加载www.example.com/后,它会转到hello函数并返回Hello World! 如何将www.example.com/Fun之类的网址指向另一个函数Fun

更新(工作代码)

更新了代码,但仍无效

import os
from flask import Flask

app = Flask(__name__)

@app.route('/')

def hello():
    return 'Hello World --  jay!'

@app.route('/fun')
def fun():
    return 'have func!'

if __name__ == '__main__':
    # Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

1 个答案:

答案 0 :(得分:3)

Decorator @ app.route为您的控制器设置网址。所以这段代码应该有效:

@app.route('/fun')
def fun():
    return 'Having Fun!'