二手产品经理

二手产品经理

THIS IS RENO

GET - 83天 - 在线学Python100天

记录#

  1. 今天是编程练习,对第 77 天的代码进行调整。
  2. 通过传递不同的参数,背景颜色显示不同。
  3. 今天的编程练习比较顺利,在编写过程中思路上是对的,只是个别地方语法还不熟练,有待加强。

CODE#

main.py#

from flask import Flask, redirect, request

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


def bgcolor(s):
    if s == 'default':
        color = 'style="background-color:#708090"'
    elif s == 'green':
        color = 'style="background-color:#90EE90"'
    elif s == 'blue':
        color = 'style="background-color:#B0C4DE"'
    else:
        color = 'style="background-color:#fff"'
    return color


@app.route('/')
def index():
    blogtitle = "R's Blog"
    link1 = "/link1?color=blue"
    link2 = "/link2?color=green"
    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', methods=["GET"])
def link1():
    s = request.args
    color = bgcolor(s["color"])
    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)
    page = page.replace("{color}", color)
    return page


@app.route('/link2', methods=["GET"])
def link2():
    s = request.args
    color = bgcolor(s["color"])
    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)
    page = page.replace("{color}", color)
    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)


index.html#

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>{blogtitle}</title>
  <link href="/static/css/portfolio.css" rel="stylesheet" type="text/css" />
</head>

<body>
  <h1>{blogtitle}</h1>
  <p><a href={link1}>Link1</a></p>
  <p><a href={link2}>Link2</a></p>
</body>

</html>

blog.html#

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Blog</title>
  <link href="/static/css/portfolio.css" rel="stylesheet" type="text/css" />
</head>

<body {color}>

  <h1>{title}</h1>
  <h2>{blogdate}</h2>
  <p>{text}</p>
<a href="{home}">Page Home</a>
  
</body>

</html>
加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。