标题有点笨拙,所以我试着解释一下。下面的代码目前的行为是这样的:它为每个url过去/ profile /生成一个页面。因此,如果我输入http://localhost:8080//profile/foo
,我就会将其转到空的个人资料页面。
我只想为我网站上存在的用户创建个人资料页面。所以我希望我的网站表现得像这样:
http://localhost:8080//profile/foo
=找不到网页
http://localhost:8080//profile/existinguser
=现有用户的页面
它的行为如下:
http://localhost:8080//profile/foo
= foo&#39页面
http://localhost:8080//profile/existinguser
=现有用户的页面
这似乎不难做到,但我对如何处理这件事感到茫然。如果有人能指出我正确的方向,我们将不胜感激。谢谢。
users = db.execute("SELECT username FROM users")
# Dynamically generate and populate profile pages for each user
# TODO Currently exists problem, there is a profile page for literally url. Needs to check against users
@app.route("/profile/<username>")
def profile(username):
user=username
elo = db.execute("SELECT elo from users WHERE username = :username", username=user)[0]["elo"]
return render_template('profile.html', users=users, user=user, elo=elo)
答案 0 :(得分:0)
找到了一种方法。代码与预期一致
query = db.execute("SELECT elo, wins, losses, draws, username from users WHERE username = :username", username=user)
# If the user does not exist, return apology
if not query:
return apology("user does not exist", 404)