Flask Sqlalchemy 'BaseQuery' 对象没有属性
Flasksqlalchemy 'BaseQuery' object has no attribute
我正在关注 this tutorial from tech with tim 有关 Flask 的内容,因此我可以了解 Flask 的一些基础知识,一切正常,直到我尝试登录用户并出现 Nonetype 错误,但我没有知道我做了什么但是然后更改变量名它给了我一个 BaseQuery has no attribute 'password'
,我一直在改变我能想到的一切,从 db 模型文件和 auth 文件以及登录 html 文件但是它没有用,这是我认为有问题的文件:
来自 auth.py:
from flask import Blueprint, render_template, request, flash, redirect, url_for
from . import db
from .models import User
from werkzeug.security import generate_password_hash, check_password_hash
auth = Blueprint('auth', __name__)
@auth.route('/login', methods=["GET", "POST"])
def login():
if request.method == "POST":
email_request = request.form.get("email_request")
password_request = request.form.get("password")
user = User.query.filter_by(email=email_request)
print(user)
if user:
if check_password_hash(user.password, password_request):
flash("Logged in successfully", category="success")
else:
flash("Incorrect password", category="error")
else:
flash("Email does not exist", category="error")
return render_template("login.html")
来自 models.py:
from . import db
from flask_login import UserMixin
from sqlalchemy.sql import func
class Note(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_note = db.Column(db.String(10000))
date = db.Column(db.DateTime(timezone=True), default=func.now())
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(130), unique=True)
password = db.Column(db.String(150))
user_name = db.Column(db.String(100))
notes = db.relationship("Note")
来自login.html:
{% extends "base.html" %}
{% block title %}Login{% endblock %}
{% block content %}
<form method="POST">
<h3 align="center">Login</h3>
<div class="form-group">
<label for="email_request">Email address</label>
<input type="email" class="form-control" id="email_request" name="email_request" placeholder="Enter email">
</div>
<div class="form-group">
<label for="passwordEnter">Password</label>
<input type="password" class="form-control" id="passwordEnter" name="passwordEnter" placeholder="Enter password">
</div>
<br/>
<button type="submit" class="btn btn-primary">Login</button>
</form>
{% endblock%}
User.query.filter_by(email=email_request)
是一个查询。
要获得第一个用户出现,您需要更正为
user = User.query.filter_by(email=email_request).first()
并且 password_request = request.form.get("password")
需要更正为 password_request = request.form.get("passwordEnter")
,因为您在 html 表格
中使用了 passwordEnter
在 auth.py
中调用 password
而不是 passwordEnter
时出错
我正在关注 this tutorial from tech with tim 有关 Flask 的内容,因此我可以了解 Flask 的一些基础知识,一切正常,直到我尝试登录用户并出现 Nonetype 错误,但我没有知道我做了什么但是然后更改变量名它给了我一个 BaseQuery has no attribute 'password'
,我一直在改变我能想到的一切,从 db 模型文件和 auth 文件以及登录 html 文件但是它没有用,这是我认为有问题的文件:
来自 auth.py:
from flask import Blueprint, render_template, request, flash, redirect, url_for
from . import db
from .models import User
from werkzeug.security import generate_password_hash, check_password_hash
auth = Blueprint('auth', __name__)
@auth.route('/login', methods=["GET", "POST"])
def login():
if request.method == "POST":
email_request = request.form.get("email_request")
password_request = request.form.get("password")
user = User.query.filter_by(email=email_request)
print(user)
if user:
if check_password_hash(user.password, password_request):
flash("Logged in successfully", category="success")
else:
flash("Incorrect password", category="error")
else:
flash("Email does not exist", category="error")
return render_template("login.html")
来自 models.py:
from . import db
from flask_login import UserMixin
from sqlalchemy.sql import func
class Note(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_note = db.Column(db.String(10000))
date = db.Column(db.DateTime(timezone=True), default=func.now())
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(130), unique=True)
password = db.Column(db.String(150))
user_name = db.Column(db.String(100))
notes = db.relationship("Note")
来自login.html:
{% extends "base.html" %}
{% block title %}Login{% endblock %}
{% block content %}
<form method="POST">
<h3 align="center">Login</h3>
<div class="form-group">
<label for="email_request">Email address</label>
<input type="email" class="form-control" id="email_request" name="email_request" placeholder="Enter email">
</div>
<div class="form-group">
<label for="passwordEnter">Password</label>
<input type="password" class="form-control" id="passwordEnter" name="passwordEnter" placeholder="Enter password">
</div>
<br/>
<button type="submit" class="btn btn-primary">Login</button>
</form>
{% endblock%}
User.query.filter_by(email=email_request)
是一个查询。
要获得第一个用户出现,您需要更正为
user = User.query.filter_by(email=email_request).first()
并且 password_request = request.form.get("password")
需要更正为 password_request = request.form.get("passwordEnter")
,因为您在 html 表格
passwordEnter
在 auth.py
password
而不是 passwordEnter
时出错