英文:
current user.is_authenticated gives me a True value but it didn't work in base.html file
问题
以下是您要翻译的内容:
我正在尝试检查用户是否已登录,以更改导航元素并执行其他操作,以下是我的代码
当我执行以下代码时
`print(attempted_account.is_authenticated)` 结果 => `True`
我认为问题可能出在 `base.html` 文件中,所以希望您可以帮助我。
**routs.py:**
@app.route('/login',methods=['GET','POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
attempted_account = Account.query.filter_by(email=form.email.data).first()
print(attempted_account.is_authenticated)
if attempted_account and attempted_account.check_password(attempted_password=form.password.data):
login_user(attempted_account)
flash('登录成功',category='success')
return redirect(url_for('page',id=1))
elif not attempted_account:
flash("该电子邮件不存在",category='danger')
else:
flash('密码不正确',category='danger')
return render_template('login.html',form=form)
**base.html**
<nav>
<ul>
<li><a class="home" href="{{ url_for('home') }}">市场</a></li>
<li><a href="{{ url_for('page',id=1) }}">购物</a></li>
</ul>
{% if current_user.is_authenticated %}
<ul>
<li><a href="#">注销</a></li>
</ul>
{% else %}
<ul>
<li><a href="{{ url_for('signup') }}">注册</a></li>
<li><a href="{{ url_for('login') }}">登录</a></li>
</ul>
{% endif %}
</nav>
**login.html**
{% extends 'base.html' %}
{% block title %}
登录
{% endblock %}
{% block content %}
<div class="container">
<form method="POST" class="form-register" style="text-align: center;">
{{ form.hidden_tag() }}
{{ form.csrf_token }}
{{ form.email.label() }}
{{ form.email(class='form-control',placeholder='example@example.com') }}
<br>
{{ form.password.label() }}
{{ form.password(class='form-control',placeholder='密码') }}
<br>
{{ form.submit(class='btn btn-lg btn-block btn-primary') }}
</form>
</div>
{% endblock %}
**请注意,我将我的模型称为 Account 而不是 User**
**编辑:**
这是 models.py:
from market import db
from market import bcrypt,login_manager
from flask_login import UserMixin
@login_manager.user_loader
def load_user(user_id):
return Account.query.filter_by(id=user_id)
class Account(db.Model,UserMixin):
id = db.Column(db.Integer,primary_key = True)
first_name = db.Column(db.String(length = 50),nullable = False)
last_name = db.Column(db.String(length = 50),nullable =False)
email = db.Column(db.String(length = 100),nullable =False,unique = True)
password_hashed = db.Column(db.String(length = 25),nullable = False)
#country = db.Column(db.String,nullable = False)
items = db.relationship('Item',backref = 'owner',lazy = True)
@property
def password(self):
return self.password
@password.setter
def password(self,plain_text_password):
self.password_hashed = bcrypt.generate_password_hash(plain_text_password).decode('utf-8')
def check_password(self,attempted_password):
return bcrypt.check_password_hash(self.password_hashed,attempted_password)
英文:
I'm trying to check if the user is logged in or not to change nav elements and to do more stuff and here is my code
And when I do this
print(attempted_account.is_authenticated)
result => True
I think that the problem will be in base.html
file so I hope you can help me.
routs.py:
@app.route('/login',methods=['GET','POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
attempted_account = Account.query.filter_by(email=form.email.data).first()
print(attempted_account.is_authenticated)
if attempted_account and attempted_account.check_password(attempted_password=form.password.data):
login_user(attempted_account)
flash('Success login ',category='success')
return redirect(url_for('page',id=1))
elif not attempted_account:
flash("The E-mail doesn't exist",category='danger')
else:
flash('Tha password is incorrect',category='danger')
return render_template('login.html',form=form)
base.html
<nav>
<ul>
<li><a class="home" href="{{ url_for('home') }}">Market</a></li>
<li><a href="{{ url_for('page',id=1) }}">Go shopping</a></li>
</ul>
{% if current_user.is_authenticated %}
<ul>
<li><a href="#">Logout</a></li>
</ul>
{% else %}
<ul>
<li><a href="{{ url_for('signup') }}">Sign up</a></li>
<li><a href="{{ url_for('login') }}">Login</a></li>
</ul>
{% endif %}
</nav>
login.html
{% extends 'base.html' %}
{% block title %}
Login
{% endblock %}
{% block content %}
<div class="container">
<form method="POST" class="form-register" style="text-align: center;">
{{ form.hidden_tag() }}
{{ form.csrf_token }}
{{ form.email.label() }}
{{ form.email(class='form-control',placeholder='example@example.com') }}
<br>
{{ form.password.label() }}
{{ form.password(class='form-control',placeholder='Password') }}
<br>
{{ form.submit(class='btn btn-lg btn-block btn-primary') }}
</form>
</div>
{% endblock %}
Please note that I called my model Account not User
Edit:
Here is models.py:
from market import db
from market import bcrypt,login_manager
from flask_login import UserMixin
@login_manager.user_loader
def load_user(user_id):
return Account.query.filter_by(id=user_id)
class Account(db.Model,UserMixin):
id = db.Column(db.Integer,primary_key = True)
first_name = db.Column(db.String(length = 50),nullable = False)
last_name = db.Column(db.String(length = 50),nullable =False)
email = db.Column(db.String(length = 100),nullable =False,unique = True)
password_hashed = db.Column(db.String(length = 25),nullable = False)
#country = db.Column(db.String,nullable = False)
items = db.relationship('Item',backref = 'owner',lazy = True)
@property
def password(self):
return self.password
@password.setter
def password(self,plain_text_password):
self.password_hashed = bcrypt.generate_password_hash(plain_text_password).decode('utf-8')
def check_password(self,attempted_password):
return bcrypt.check_password_hash(self.password_hashed,attempted_password)
答案1
得分: 1
问题可能是因为您在user_loader
回调中返回了一个查询结果,而不是一个Account
对象。
尝试返回Account.query.filter_by(id=user_id).first()
,而不是返回Account.query.filter_by(id=user_id)
。
希望能解决问题。
英文:
The issue is probably because you're returning a query result in the user_loader
callback instead of an Account
object.
Instead of returning Account.query.filter_by(id=user_id)
try returning Account.query.filter_by(id=user_id).first()
.
Hope it works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论