finance - cs50 财务,加载到服务器时找不到 404 错误,注册时出现 500 内部服务器错误
问题描述
每当我在运行烧瓶后第一次加载到网站时,我都会收到“404 not found”错误,我不知道如何将其更改为仅显示登录页面。
当我尝试注册一个新用户时,信息确实会被记录并在数据库中散列,但我收到一个 500 内部服务器错误,如果我使用登录页面,我只能访问主页。我不确定我应该如何解决这个问题,所以非常感谢任何帮助。
应用程序.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")))
我决定将我的“确认密码”重命名为“确认密码”,只是为了保持代码中的某种程度的相似性。但更重要的是,当我改用这些代码行时,它起作用了:
# 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 not found 错误
推荐阅读
- google-admin-sdk - 审计报告 API 限制增加
- c# - 我应该在哪个级别测试我的逻辑
- asp.net-core-2.0 - Kestrel 在哪里寻找要监听的端口?
- node.js - 带有 nginx 配置的 socket.io
- vue.js - Vue JS表格标签中如何正确使用v-if和v-else
- php - 我有多个与数组同名且值相同的单选按钮
- symfony - Symfony4 安全中的两种认证方式
- php - 在 wordpress 中找不到函数“remove_comment_support”或函数名无效
- mongodb - 分片的 mongodb 集群只使用一个分片
- azure - 将 Sparkfun ESP8266 Thing 连接到 Azure IoT Hub