我正在做一个项目。之后,我设置flask-login
系统时遇到错误。
错误:
TypeError:“ BaseQuery”对象不可调用
删除flask-login system
时。它正在工作,但我需要使用。
@login_manager.user_loader
def load_user(user_id):
return Login.query(user_id) # <<------------- Problem is here --------------->>
如果我使用Login.query(user_id)
代替db.session.query(Login).filter_by(id=user_id)
。
AttributeError:“ BaseQuery”对象没有属性“ is_authenticated”
我会尝试找出我在做错什么,但仍然不知道?
Model.py
from flask_login import UserMixin
from run import db, date
class Login(db.Model, UserMixin):
__tablename__ = 'login'
id = db.Column('No', db.Integer, primary_key=True)
name = db.Column('Username', db.String(50), nullable=False)
email = db.Column('Email', db.String(100), nullable=False)
password = db.Column('Password', db.String(50), nullable=False)
date = db.Column('Date', db.String(20), nullable=False, default=date)
@property
def is_authenticated(self):
return True
@property
def is_active(self):
return True
@property
def is_anonymous(self):
return True
@property
def get_id(self):
return self.id
@property
def __repr__(self):
return '<Login(name=%s, email=%s, password=%s, date=%s)>' % (self.name, self.email, self.password, self.date)
route.py
from flask import render_template, redirect, url_for, request, flash
from model import Post, Contacts, Login_form, Signup_form, Login
from flask_login import login_required, login_user, current_user
from run import app, db, login_manager
# user id loader <<--------------- Problem is here 'flask-login query' ---------------------->>
@login_manager.user_loader
def load_user(user_id):
return Login.query(user_id)
@app.route('/')
@login_required
def index():
posts = db.session.query(Post).all()
return render_template('index.html', posts=posts)
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/add_post')
@login_required
def add_posts():
return render_template('add_post.html')
@app.route('/post')
@login_required
def post():
all_post = db.session.query(Post).all()
return render_template('post.html', posts=all_post)
@app.route('/contact', methods=['POST','GET'])
@login_required
def contact():
if request.method=='POST':
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
msg = request.form['message']
contact_form = Contacts(naem=name, email=email, phone=phone, message=msg)
db.session.add(contact_form)
db.session.commit()
return redirect(url_for('index'))
else: return render_template('contact.html')
@app.route('/<int:no>/post')
@login_required
def post_direction(no):
posts = db.session.query(Post).filter_by(id=no).first()
return render_template('post.html', posts=posts)
@app.route('/login', methods=['POST', 'GET'])
def login():
log_in = Login_form()
if log_in.validate_on_submit():
if log_in.email.data !='' and log_in.password.data !='':
check = db.session.query(Login).filter_by(email=log_in.email.data)
if check and check.password == log_in.password.data:
login_user(check, remember=log_in.remember.data)
return redirect(request.args.get('next'), 'index')
else:
flash(u'Email and password not matching!', 'Invalid')
else:
flash(u'Invalid email1 and password!', 'Invalid')
else:
return render_template('login.html', Login_form=log_in)
@app.route('/signup', methods=['POST', 'GET'])
def signup():
return render_template('signup.html', Signup_form=Signup_form('/signup'))
# unauthorized handle
@login_manager.unauthorized_handler
def unauthorized():
return redirect(url_for('login'))
@app.errorhandler(404)
def error(e):
return render_template('404.html')
run.py
from flask import Flask
from flask_admin import Admin
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_bootstrap import Bootstrap
app = Flask(__name__, static_folder='static', template_folder='template')
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost/flask'
upload_path = './static/img'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['UPLOAD_FOLDER'] = upload_path
app.config['SECRET_KEY'] = 'super_secret'
db = SQLAlchemy(app)
admin = Admin(app, template_mode='bootstrap3')
bootstrap = Bootstrap(app)
login_manager = LoginManager(app)
login_manager.init_app(app)
from route import *
if __name__ == '__main__':
app.run(debug=True)
index.html
<html>
.......
</html>
答案 0 :(得分:1)
要获取ID,请使用:
Login.query.get(user_id)
Login.query
是db.session.query(Login)
的快捷方式,不可调用。
有关以下信息,请参见Flask-SQLAlchemy文档: