1

这是我的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! How to point a url like www.example.com/Funto another functionFun

更新(工作代码)

更新了代码但仍然无法正常工作

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)
4

1 回答 1

3

装饰器 @app.route 为您的控制器设置 url。所以这段代码应该可以工作:

@app.route('/fun')
def fun():
    return 'Having Fun!'
于 2013-01-15T09:52:51.397 回答