提问人:Root 提问时间:11/17/2023 最后编辑:Root 更新时间:11/18/2023 访问量:76
**kwargs 的格式,单位为 db。Model.query.filter_by()
Format of **kwargs in db.Model.query.filter_by()
问:
我有一个定义如下的类:users
class users(db.Model):
name = db.Column('name', db.String(100), primary_key=True)
password = db.Column('password', db.String(100))
role = db.Column('role', db.String(100))
def __init__(self, name: str, password: str, role: str):
self.name = name
self.password = password
self.role = role
我正在尝试创建一个登录表单,成功登录需要将用户名与密码匹配。为此,我尝试了以下查询:
user = request.form['Username']
password = request.form['Password']
found_user = users.query.filter_by(name=user, password=password).first()
但是,这种方法并没有像预期的那样工作。我收到一个错误,指出该类不可下标,阻止我使用访问每行的元素。我不确定如何根据匹配的用户名和密码对用户进行身份验证。有关如何在此方案中正确构建查询或处理身份验证的任何指导将不胜感激。users
[]
编辑:
我想我已经找到了问题:
我尝试删除SQLAlchemy类代码,并改用以下代码:
from flask import Flask,render_template,request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.app_context().push()
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///db.sqlite3'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
db = SQLAlchemy(app)
class users(db.Model):
name = db.Column('name',db.String(100),primary_key=True)
password = db.Column('password',db.String(100))
role = db.Column('role',db.String(100))
def __init__(self,name:str,password:str,role:str):
self.name = name
self.password = password
self.role = role
with app.app_context():
db.create_all()
admin = users('Admin','RootRules99','Admin')
loggedInUsr:users
db.session.add(admin)
db.session.commit()
@app.route('/',methods=['POST','GET'])
def frontPage():
if request.method=='POST':
user = request.form['Username']
password = request.form['Password']
return user,password
else:
return render_template('mywebsite.html')
使用此 HTML 网页:
<style>
button
{
font-family: Verdana, Geneva, Tahoma, sans-serif;
font-size: medium;
color:black;
background-color: aliceblue;
border-style: hidden;
shape-outside: border-box;
cursor: default;
height: 36px;
width: 144px;
border-radius: 9px;
top: 500px;
left: 680px;
position: absolute;
}
button:hover
{
background-color: orangered;
}
</style>
<head>
<title>Sign in/Sign up</title>
</head>
<body background="backColorWebsite.png">
<form action="#" method="post">
<input type="text" id="Username" value="Username" style="font-
family:Verdana, Geneva, Tahoma, sans-serif; background-color:
aliceblue; position: absolute; top:250px;left:680px; width:144px;
height:27px; border-radius: 9px; color:gray" maxlength="10">
</input>
<input type="password" id="Password" value="Password" style="font-
family:Verdana, Geneva, Tahoma, sans-serif; background-color:
aliceblue; position: absolute; top:350px;left:680px; width:144px;
height:27px; border-radius: 9px; color:gray" maxlength="10">
</input>
<button>Sign In</button>
</body>
如果我输入凭据“Hi”和“Admin”,网站只会打印出“Hi”,这很奇怪,因为它根本没有考虑“Admin”这个词。我应该如何解决这个问题?
答:
0赞
Detlef
11/18/2023
#1
使用在输入字段的 name 属性中定义的键查询表单字段数据。您尚未定义 name 属性,因此不会接收任何数据。
此外,表单元素在 HTML 代码中未正确关闭。
from flask import Flask,render_template,request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///db.sqlite3'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
db = SQLAlchemy(app)
class User(db.Model):
__tablename__ = 'users'
id = db.Column(Integer, primary_key=True)
username = db.Column(db.String(100), unique=True)
password = db.Column(db.String(100))
with app.app_context():
db.create_all()
admin = User(
username='Admin',
password='RootRules99'
)
db.session.add(admin)
db.session.commit()
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
print(username, password)
return render_template('index.html')
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sign in/Sign up</title>
</head>
<body>
<form method="post">
<div>
<label for="username">Username</label>
<input type="text" id="username" name="username" />
</div>
<div>
<label for="password">Password</label>
<input type="password" id="password" name="password" />
</div>
<button type="submit">Sign In</button>
</form>
</body>
</html>
评论
request.form['Username']
request.form['Password']
<input>
name
<input id="..." name="username"...>