iJade
iJade

Reputation: 23801

Defining a different route for python app

Here is my python code

    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)

On loading the www.example.com/ it goes to the hello function and returns Hello World! How to point a url like www.example.com/Fun to another function Fun

Update(Working Code)

Updated code but still not working

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)

Upvotes: 1

Views: 4340

Answers (1)

zimyand
zimyand

Reputation: 68

Decorator @app.route sets url for your controller. So this code should work:

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

Upvotes: 3

Related Questions