External Exam Download Resources Web Applications Games Recycle Bin

URL Routing

urlroute1.py

from flask import *
app = Flask(__name__)

@app.route("/")
def start():
  return "<a href='/here'>click to go</a>"

@app.route("/here")
def here():
  return "you are now here!"
    
app.run(debug=True)

urlroute2.py

from flask import *
app = Flask(__name__)

news = "News. link to: <a href='/weather'>weather</a>"
weather = "Weather. link to: <a href='/news'>news</a>"

#YOU CAN ROUTE 2 URLs TO THE SAME FUNCTION:

@app.route("/")
@app.route("/news")
def giveNews():
  return news

@app.route("/weather")
def giveWeather():
  return weather
    
app.run(debug=True)

urlroute3.py

from flask import *
app = Flask(__name__)

@app.route("/")
def start():
  return "<a href='/here'>go here</a>"

#URL Processors can read URLs:

@app.route("/<where>")
def gone(where):
  return "You went: " + where
    
app.run(debug=True)

urlroute4.py

from flask import *
app = Flask(__name__)

html = """
<a href='/user/mary'>login as user mary</a>
<a href='/admin/sarah'>login as admin sarah</a>
"""

@app.route("/")
def start():
  return html

#You can have multiple URL Processors:

@app.route("/<level>/<name>")
def login(level, name):
  return "level: " + level + ",name: " + name
    
app.run(debug=True)

urlroute5.py

from flask import *
app = Flask(__name__)

@app.route('/')
def incoming():
    return redirect('/home')

@app.route('/home')
def home():
    return "<a href='/digisoln'>digisoln</a>"

@app.route('/digisoln')
def digisoln():
    
    ##########################################
    ## you can also redirect to other URLS: ##
    ##########################################
    
    return redirect("https://www.digisoln.com/")
    
app.run(debug=True)