二手产品经理

二手产品经理

THIS IS RENO

flask - 86 days - Learn Python online for 100 days

Record#

  1. I haven't been feeling well and have been busy with too many things in the past two weeks, which has caused a lot of delays in this learning plan. I will catch up starting today!
  2. Today's exercise is to create a personal blog page with login and publishing functionality.
  3. There were several issues encountered during this exercise:
    1. When redirecting, use "return".
    2. When storing data in the database, if the value is "", it will be treated as a string because it is wrapped in quotes. Therefore, when storing in JSON format, there must be no quotes on the outside.
    3. The indentation of the code after a loop or if statement must be correct, and understanding of the for loop needs to be strengthened.

CODE#

main.py#

from flask import Flask, request, redirect, session
from replit import db
import os
from datetime import datetime

app = Flask(__name__)
app.secret_key = os.environ['sission_key']


@app.route('/', methods=['GET'])
def index():
    #db['user'] = {'name': 'reno', 'pass': '123'}
    if session.get('name'):
        text1 = ""
        f = open("index.html", "r")
        page = f.read()
        f.close()
        for key in db.keys():
            if key and key != "user":
                name = db[key]
                text = f"""
                </hr>
                <h2>{name["title"]}</h2>
                <p>{key}</p>
                <p>{name["text"]}</p>
                """
                text1 += text
        page = page.replace("{text}", text1)
        return page
    else:
        return redirect('/log')


@app.route('/', methods=["POST"])
def submit():
    text = request.form
    ttime = datetime.now()
    title = text["title"]
    text = text["text"]
    db[ttime] = {"title": title, "text": text}
    return redirect('/')


@app.route('/log1')
def log1():
    return f"page{session.get('name')}"


@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
    vuser = db['user']
    if vuser['name'] == user["name"] and vuser['pass'] == user["pass"]:
        session["name"] = vuser['name']
        print("--")
        print(session.get('name'))
        print("--")
        return redirect("/")
    else:
        return "NAME OR PASS ERROR!"


app.run(host='0.0.0.0', port=81)

index.html#

<html>
  <head>
    <title>MY BLOG</title>
  </head>
  <body>
    <h1>MY BLog</h1>
    <hr />
    <form method="post" action="/">
      <p>Title: <input type="text" name="title"></p>
      <p>Text: <input type="text" name="text"></p>
     <button>submit</button>
    </form>
     <p>{text}</p>
  </body>
</html>

login.html#

<form method="post" action="/login">
  <p>Name: <input name="name" type="text" ></p>
  <p>Pass: <input name="pass" type="text" ></p>
  <p><button name="submit" value="submit">submit</button></p>
</form>
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.