Record#
- Today I connected the previous knowledge points together. First, I worked on database operations, and then I worked on recent request operations. This also involved operations with dictionaries and arrays.
- In replit, the content in the database needs to be stored first, which means db[]=string. Then it can be read. The inconvenient part is that this storing code needs to be commented out in subsequent runs.
- I still don't understand
static_url_path='/static'
completely. 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 modify passwords, but in the beginning part of the video, it didn't specify which user's password to modify. It works fine when there is only one user 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.
- In the exercise of troubleshooting errors, I found a bug in the code. 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)