cs50 finance,加载到服务器时出现 404 未找到错误,注册时出现 500 内部服务器错误

cs50 finance, 404 not found error upon loading into server and 500 internal server error for register

每当我在 运行 flask 之后首次加载到网站时,我都会收到“404 not found”错误,我不确定如何将其更改为只显示登录页面。

当我尝试注册一个新用户时,信息确实被记录并散列在数据库中,但我收到一个 500 内部服务器错误,如果我使用登录页面,我只能访问主页。我不确定我应该如何解决这个问题,因此非常感谢对此的任何帮助。

application.py(登录)

@app.route("/login", methods=["GET", "POST"])
def login():
    """Log user in"""

    # Forget any user_id
    session.clear()

    # User reached route via POST (as by submitting a form via POST)
    if request.method == "POST":

        # Ensure username was submitted
        if not request.form.get("username"):
            return apology("must provide username", 403)

        # Ensure password was submitted
        elif not request.form.get("password"):
            return apology("must provide password", 403)

        # Query database for username
        rows = db.execute("SELECT * FROM users WHERE username = :username",
                          username=request.form.get("username"))

        # Ensure username exists and password is correct
        if len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("password")):
            return apology("invalid username and/or password", 403)

        # Remember which user has logged in
        session["user_id"] = rows[0]["id"]

        # Redirect user to portfolio page
        return redirect("/portfolio")

    # User reached route via GET (as by clicking a link or via redirect)
    else:
        return render_template("login.html")

application.py(注册)

@app.route("/register", methods=["GET", "POST"])
def register():
    """Register user"""

    # Forget any user_id
    session.clear()

    # User reached route via POST (as by submitting a form via POST)
    if request.method == "POST":

        # Ensure username was submitted
        if not request.form.get("username"):
            return apology("must provide username", 403)

        # Ensure password was submitted
        elif not request.form.get("password"):
            return apology("must provide password", 403)

        # Ensure confirm password is submitted        
        elif not request.form.get("confirm-password"):
            return apology("must confirm password", 403)

        # if confirm password does not match password
        elif request.form.get("password") != request.form.get("confirm-password"):
            return apology("must match new password", 403)

        # update database for username and hash new password
        rows = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)", 
        username=request.form.get("username"), hash=generate_password_hash(request.form.get("password")))

        # Give user id number
        session["user_id"] = rows[0]["id"]

        # Redirect user to portfolio page
        return redirect("/portfolio")

    # User reached route via GET (as by clicking a link or via redirect)
    else:
        return render_template("register.html")

对于register.html

{% extends "layout.html" %}

{% block title %}
    Register
{% endblock %}

{% block main %}
    <form action="/register" method="post">
        <div class="form-group">
            <input autocomplete="off" autofocus class="form-control" name="username" 
            placeholder="Username" type="text">
        </div>
        <div class="form-group">
            <input class="form-control" name="password" placeholder="Password" type="password">
        </div>
        <div class="form-group">
            <input class="form-control" name="confirm-password" placeholder="Confirm Password" 
            type="password">
        </div>
        <button class="btn btn-primary" type="submit">Register</button>
    </form>
{% endblock %}

没关系,我想出了寄存器部分。

我把这条线拿出来了

# update database for username and hash new password
        rows = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)", 
        username=request.form.get("username"), hash=generate_password_hash(request.form.get("password")))

我决定将我的 'confirm-password' 重命名为 'confirm_password' 只是为了在代码中保持某种程度的相似性。 但更重要的是,当我改用这些代码行时它起作用了:

 # if confirm password does not match password
        elif request.form.get("password") != request.form.get("confirm_password"):
            return apology("password does not match")

        # Hash password / Store password hash_password =
        hashed_password = generate_password_hash(request.form.get("password"))

        # Add user to database
        result = db.execute("INSERT INTO users (username, hash) VALUES(:username, :hash)",
                username = request.form.get("username"),
                hash = hashed_password)

        if not result:
            return apology("The username is already taken")

        rows = db.execute("SELECT * FROM users WHERE username = :username",
                  username = request.form.get("username"))

我最近还发现,如果我这样做是因为我想将 index(portfolio link) 与 /(homepage link):

分开
@app.route("/")
def start():
    return render_template("login.html")

@app.route(/index) 部分之前

此外,我在 layout.html

的第 29 行添加了
<a class="navbar-brand" href="/"><span class="blue">C</span><span class="red">$</span><span class="yellow">5</span><span class="green">0</span> <span class="red">Finance</span></a>

它前后的这些附加行。这样当我点击主页按钮后已经登录时它不会把我带到登录页面:

            {% if session.user_id %}
                <a class="navbar-brand" href="/portfolio"><span class="blue">C</span><span class="red">$</span><span class="yellow">5</span><span class="green">0</span> <span class="red">Finance</span></a>
            {% else %}
                <a class="navbar-brand" href="/"><span class="blue">C</span><span class="red">$</span><span class="yellow">5</span><span class="green">0</span> <span class="red">Finance</span></a>
            {% endif %}

我不再遇到 404 未找到错误