#記錄
- 今天是編程練習,對第 77 天的代碼進行調整。
- 通過傳遞不同的參數,背景顏色顯示不同。
- 今天的編程練習比較順利,在編寫過程中思路上是對的,只是個別地方語法還不熟練,有待加強。
#代碼
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>