二手产品经理

二手产品经理

THIS IS RENO

falsk - 77 days - Learn Python online for 100 days

Record#

  1. Today I continued to study the knowledge related to flask.
  2. About the use of templates: In the flask route, we can use the f function to read the local HTML file and return its content to the page. If there are variables that need to be replaced in the HTML file, we can use the replace function to replace the identifier string and the corresponding variable value.
  3. About the redirect function: It is another function provided by flask for page redirection. I still don't quite understand the purpose of this redirection.
  4. During the learning process, I discovered an important point: when the flask code is running, it will first look for the index() route function, and if it is not found, it will return a 404 error.
  5. Today's exercise is to write a blog page, and we will use templates and the redirect function to complete it.

CODE#

from flask import Flask, redirect

app = Flask(__name__, static_url_path="/static")


@app.route('/')
def index():
    blogtitle = "R's Blog"
    link1 = "/link1"
    link2 = "/link2"
    f = open("template/index.html", "r")
    page = f.read()
    f.close
    page = page.replace("{blogtitle}", blogtitle)
    page = page.replace("{link1}", link1)
    page = page.replace("{link2}", link2)
    return page


@app.route('/link1')
def link1():
    title = "link1"
    blogdate = "Day 56 Solution"
    text = "texttexttexttexttexttexttexttexttexttexttext"
    home = "/home"
    page = ""
    f = open("template/blog.html", "r")
    page = f.read()
    f.close()
    page = page.replace("{title}", title)
    page = page.replace("{blogdate}", blogdate)
    page = page.replace("{text}", text)
    page = page.replace("{home}", home)
    return page


@app.route('/link2')
def link2():
    title = "link2"
    blogdate = "Day 57 Solution"
    text = "texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext"
    home = "/home"
    page = ""
    f = open("template/blog.html", "r")
    page = f.read()
    f.close()
    page = page.replace("{title}", title)
    page = page.replace("{blogdate}", blogdate)
    page = page.replace("{text}", text)
    page = page.replace("{home}", home)
    return page


@app.route("/home")
def seventySeven():
    return redirect("https://replit.com/@DengLe/python100day77#main.py")


app.run(host='0.0.0.0', port=81)
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.