问题是:
每当我试图从ui注册用户时,都会收到一个错误。我们的想法是提交表单并将数据导入python,然后运行insert查询,以便将其添加到数据库中以允许登录。
样本输入:
姓名:基隆
密码(散列):pbkdf2:sha256:150000$aqfm996v$af032bc39170c1e2b59cab53691eb48e50d3824b851de6921c56f07f4a10d3c
电子邮件:kieron@example.com
错误消息:
> sqlalchemy.exc.StatementError: (sqlalchemy.exc.InvalidRequestError) A
> value is required for bind parameter 'password'
>[SQL: INSERT INTO
> users (name, password, email) VALUES(%(user)s, %(password)s,
> %(email)s)] [parameters: [{'user': 'Kieron', ':password':
> 'pbkdf2:sha256:150000$aQfm996V$af032bc39170c1e2b59cab53691ebb48e50d3824b851de6921c56f07f4a10d3c',
> 'email': 'kieron@example.com'}]]
数据库连接:
数据库与sqlalchemy引擎绑定连接,而不使用orm,目的是了解更多关于在没有orm的情况下进行绑定的信息,应用程序配置如下:
app = Flask(__name__)
app.config['DATABASE_URL']="url to db"
# Check for environment variable
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
与问题相关的代码:
我相信下面的路线是问题的根源,我不太确定我在其中哪里出错了。路线的整体思路是:
清除上一个会话并从表单中获取详细信息。
对数据库运行表单条目以确保条目的唯一性。
创建密码散列并输入数据库。
@app.route("/register", methods=['POST','GET'])
def register():
session.clear()
if request.method == 'POST':
#check for empty fields
if not request.form.get("name"):
return apology("must provide username", 403)
elif not request.form.get("password"):
return apology("must provide password", 403)
elif not request.form.get("email"):
return apology("must provide email", 403)
#get variables from form
user = request.form.get("name")
password = request.form.get("password")
email = request.form.get("email")
#hash password
passwordHash = generate_password_hash(password, method='pbkdf2:sha256', salt_length=8)
#check email and username don't exist
if db.execute("SELECT name FROM users WHERE name = :user", {"user": user}).rowcount != 0:
return apology("Username already taken", 403)
elif db.execute("SELECT email FROM users WHERE email = :email", {"email": email}).rowcount != 0:
return apology("Email already has an account in system",403)
#add to db
db.execute("INSERT INTO users (name, password, email) VALUES(:user, :password, :email)", {"user":user, "password":passwordHash, "email":email})
db.commit()
return redirect("/login")
return render_template("register.html")
问题摘要:
我相信错误来自insert查询,但是我不确定在哪里。
我哪里做错了?我怎样才能使它正确工作?
1条答案
按热度按时间roejwanj1#
要插入的值中有一个输入错误。
当前代码无效
{"user":user, ":password":passwordHash, "email":email}
尽管它应该是{"user":user, "password":passwordHash, "email":email}
(已删除):
之前password
)