Record#
- Today I connected the previous knowledge points together. First, I worked on database operations, and then I focused on recent request operations. This also involved operations with dictionary arrays and more.
- In replit, the content in the database needs to be stored first, which means db[]=string. Then it can be retrieved. The inconvenient part is that the code for storing this needs to be commented out in subsequent runs.
- I still don't quite understand
static_url_path='/static'
. AI explains it like this:static_url_path
is mainly used to organize the URL structure better, making the code more readable and maintainable. It is also possible to not set this parameter. - I learned how to change passwords, but at the beginning of the video, it didn't specify which user's password to change. It works fine when there is only one in the database, but it's wrong if there are multiple users. When writing code, it's important to avoid this kind of problem and consider multiple users.
- During the debugging exercise, I found a bug in the code where I can change the password of other users. Hahaha!
CODE#
from flask import Flask, request, redirect
from replit import db
app = Flask(__name__, static_url_path='/static')
@app.route("/")
def index():
page = """
<p><a href="/sign">Sign Up</a></p>
<p><a href="/log">Log In</a></p>
"""
return page
@app.route("/sign")
def sign():
f = open("sign.html", "r")
page = f.read()
f.close
return page
@app.route("/signup", methods=["POST"])
def signup():
user = request.form
if user["username"] not in db.keys():
db[user["username"]] = {
"username": user["username"],
"name": user["name"],
"password": user["password"]
}
page = f"Hello {user['name']}"
else:
page = f"{user['username']} exists"
return page
@app.route("/log")
def log():
f = open("login.html", "r")
page = f.read()
f.close
return page
@app.route("/login", methods=["POST"])
def login():
user = request.form
print(f"""
{db[user["username"]]["username"]} == {user["username"]} and {db[
user["username"]]["password"]} == {user["password"]}:
""")
if db[user["username"]]["username"] == user["username"] and db[
user["username"]]["password"] == user["password"]:
page = f"Hello {db[user['name']]}"
else:
page = "Username Or Password error"
return page
app.run(host='0.0.0.0', port=81)