Routing and URL Rules
The routing is the rule: the @app.route (the path, the view, the no the 1), the converter (the 5, the int, the str, the slug, the path), and the method (the GET, the POST, the no the 1). The endpoint is the function (the name, the no the 1), the url_for is the reverse (the endpoint, the no the hardcode), and the redirect is the trailing (the 308, the 1, the no the 1). This chapter covers the route (the path, the view), the converter (the 5, the no the 1), the method (the GET, the POST), the endpoint (the no the 1), and the url_for (the no the hardcode).
The route and the 5 converter
# the ROUTE (the path, the view,
# the no the 1):
from flask import Flask
app = Flask(__name__)
# the 1: the no (the no,
# the no, the no
# the no, the no
# the no):
@app.route("/")
def index():
return "the 1"
# the 2: the 5 (the int, the
# str, the slug, the uuid,
# the path, the no the 1):
@app.route("/post/<int:id>")
def post_detail(id):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
return f"the post {id}"
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no)
@app.route("/post/<slug:slug>")
def post_by_slug(slug):
# the no: the no (the no,
# the no, the no
# the no, the no
# the no):
return f"the {slug}"
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the 2 (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
The route is the rule: the @app.route("/") is the 1 (the path, the view, the no the 1), and the function is the endpoint (the name, the no the 1) - the 5 converter is the no the 1: the int (the 1, the 2, the no the abc, the no the the), the str (the no the /, the default, the no the 1), the slug (the x-y-z, the no the space, the 1 the the), the uuid (the 8-4-4-4-12, the no the the), and the path (the 1, the /, the 1 the the, the no the the) - the no is the 1 (the 1, the no the 1, the no the the), and the method is the next: the GET the POST the no the 1.
The method, the endpoint, and the url_for
# the METHOD (the GET, the POST,
# the no the 1, the no the 2):
@app.route("/post/",
methods=["GET", "POST"])
def post_create():
from flask import request
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
if request.method == "POST":
body = request.form.get(
"body", "").strip()
# the no: the no (the
# no, the no, the no
# the no, the no
# the no)
# the 1: the redirect
# (the 302, the no
# the 1, the no
# the the):
from flask import redirect, \
url_for
return redirect(
url_for("index"))
return "the form"
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the ENDPOINT (the function, the
# no the 1, the no the 1):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
@app.route("/the-1/",
endpoint="the_1")
def view_the_1():
return "the 1"
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the URL_FOR (the reverse, the
# no hardcode, the no the 1):
# the no: the 1 (the no,
# the no, the no
# the no, the no
# the no):
url_for("post_detail", id=42)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
# the no: the no (the no,
# the no, the no
# the no, the no
# the no)
The method is the 2: the GET is the 1 (the no, the no the 1), and the POST is the 1 (the form, the no the 1) - the methods=["GET", "POST"] is the 2 (the 1, the no the 1), and the no is the 405 (the method, the no the the) - the redirect is the PRG (the POST the 302 the GET, the no the 1, the no the the). The endpoint is the function: the name is the 1 (the no the 1, the no the the), and the endpoint= is the no (the 2 the 1, the no the the) - the url_for is the reverse: the endpoint plus the arg is the 1 (the no the hardcode, the no the 1, the no the the) - the rule is the one: the route the rule, the 5 the no the 1, the method the 2, the endpoint the no the 1, and the url_for the no the hardcode - the next chapter's request is the GET the POST the json the no the 1.
The Working Rules
- The route is the rule, the function the endpoint. The @app.route("/") the 1 (the path, the view, the no the 1), and the function the name (the no the 1) - the no the 1 (the 1, the no the 1), and the endpoint= the no (the 2 the 1, the no the the).
- The 5 converter is the no 1, the 404 the no. The int the 1 the 2 (the no the abc, the no the the), and the str the no the / (the default) - the slug the x-y-z (the no the space), the uuid the 8-4-4-4-12, and the path the 1 the / (the no the the), and the no the 1 (the 1, the no the 1).
- The method is the 2, the 405 the no. The GET the 1 (the no the 1), and the POST the 1 (the form, the no the 1) - the methods the 2 (the 1, the no the 1), and the no the 405 (the method, the no the the), and the redirect the PRG (the 302, the no the 1).
- The url_for is the reverse, the no hardcode. The endpoint plus the arg the 1 (the no the 1, the no the the), and the 1 the no (the no the 1, the no the the) - the no the 1 (the no the 1, the no the the), and the no the the (the no the 1, the no the the).
Summary
The routing is the rule: the @app.route the 1 (the path, the view, the no the 1, the function the endpoint, the no the 1, the endpoint= the no the 2 the 1), the 5 converter the no the 1 (the int the no the abc, the str the no the /, the slug the x-y-z, the uuid the 8-4-4-4-12, and the path the 1 the /), the method the 2 (the GET the 1, the POST the 1, the methods the 2, the no the 405, the redirect the PRG the 302), and the url_for the no the hardcode (the endpoint plus the arg the 1, the no the 1). The rule is the one: the route the rule, the 5 the no the 1, the method the 2, the endpoint the no the 1, and the url_for the no the hardcode - the next chapter's request and response is the 5: the GET, the POST, the json, the files, and the headers (the no the 1, the no the the).