current user.is_authenticated gives me a True value but it didn’t work in base.html file.

huangapple go评论148阅读模式
英文:

current user.is_authenticated gives me a True value but it didn't work in base.html file

问题

以下是您要翻译的内容:

  1. 我正在尝试检查用户是否已登录,以更改导航元素并执行其他操作,以下是我的代码
  2. 当我执行以下代码时
  3. `print(attempted_account.is_authenticated)` 结果 => `True`
  4. 我认为问题可能出在 `base.html` 文件中,所以希望您可以帮助我。
  5. **routs.py:**
  6. @app.route('/login',methods=['GET','POST'])
  7. def login():
  8. form = LoginForm()
  9. if form.validate_on_submit():
  10. attempted_account = Account.query.filter_by(email=form.email.data).first()
  11. print(attempted_account.is_authenticated)
  12. if attempted_account and attempted_account.check_password(attempted_password=form.password.data):
  13. login_user(attempted_account)
  14. flash('登录成功',category='success')
  15. return redirect(url_for('page',id=1))
  16. elif not attempted_account:
  17. flash("该电子邮件不存在",category='danger')
  18. else:
  19. flash('密码不正确',category='danger')
  20. return render_template('login.html',form=form)
  21. **base.html**
  22. <nav>
  23. <ul>
  24. <li><a class="home" href="{{ url_for('home') }}">市场</a></li>
  25. <li><a href="{{ url_for('page',id=1) }}">购物</a></li>
  26. </ul>
  27. {% if current_user.is_authenticated %}
  28. <ul>
  29. <li><a href="#">注销</a></li>
  30. </ul>
  31. {% else %}
  32. <ul>
  33. <li><a href="{{ url_for('signup') }}">注册</a></li>
  34. <li><a href="{{ url_for('login') }}">登录</a></li>
  35. </ul>
  36. {% endif %}
  37. </nav>
  38. **login.html**
  39. {% extends 'base.html' %}
  40. {% block title %}
  41. 登录
  42. {% endblock %}
  43. {% block content %}
  44. <div class="container">
  45. <form method="POST" class="form-register" style="text-align: center;">
  46. {{ form.hidden_tag() }}
  47. {{ form.csrf_token }}
  48. {{ form.email.label() }}
  49. {{ form.email(class='form-control',placeholder='example@example.com') }}
  50. <br>
  51. {{ form.password.label() }}
  52. {{ form.password(class='form-control',placeholder='密码') }}
  53. <br>
  54. {{ form.submit(class='btn btn-lg btn-block btn-primary') }}
  55. </form>
  56. </div>
  57. {% endblock %}
  58. **请注意,我将我的模型称为 Account 而不是 User**
  59. **编辑:**
  60. 这是 models.py
  61. from market import db
  62. from market import bcrypt,login_manager
  63. from flask_login import UserMixin
  64. @login_manager.user_loader
  65. def load_user(user_id):
  66. return Account.query.filter_by(id=user_id)
  67. class Account(db.Model,UserMixin):
  68. id = db.Column(db.Integer,primary_key = True)
  69. first_name = db.Column(db.String(length = 50),nullable = False)
  70. last_name = db.Column(db.String(length = 50),nullable =False)
  71. email = db.Column(db.String(length = 100),nullable =False,unique = True)
  72. password_hashed = db.Column(db.String(length = 25),nullable = False)
  73. #country = db.Column(db.String,nullable = False)
  74. items = db.relationship('Item',backref = 'owner',lazy = True)
  75. @property
  76. def password(self):
  77. return self.password
  78. @password.setter
  79. def password(self,plain_text_password):
  80. self.password_hashed = bcrypt.generate_password_hash(plain_text_password).decode('utf-8')
  81. def check_password(self,attempted_password):
  82. 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:

  1. @app.route(&#39;/login&#39;,methods=[&#39;GET&#39;,&#39;POST&#39;])
  2. def login():
  3. form = LoginForm()
  4. if form.validate_on_submit():
  5. attempted_account = Account.query.filter_by(email=form.email.data).first()
  6. print(attempted_account.is_authenticated)
  7. if attempted_account and attempted_account.check_password(attempted_password=form.password.data):
  8. login_user(attempted_account)
  9. flash(&#39;Success login &#39;,category=&#39;success&#39;)
  10. return redirect(url_for(&#39;page&#39;,id=1))
  11. elif not attempted_account:
  12. flash(&quot;The E-mail doesn&#39;t exist&quot;,category=&#39;danger&#39;)
  13. else:
  14. flash(&#39;Tha password is incorrect&#39;,category=&#39;danger&#39;)
  15. return render_template(&#39;login.html&#39;,form=form)

base.html

  1. &lt;nav&gt;
  2. &lt;ul&gt;
  3. &lt;li&gt;&lt;a class=&quot;home&quot; href=&quot;{{ url_for(&#39;home&#39;) }}&quot;&gt;Market&lt;/a&gt;&lt;/li&gt;
  4. &lt;li&gt;&lt;a href=&quot;{{ url_for(&#39;page&#39;,id=1) }}&quot;&gt;Go shopping&lt;/a&gt;&lt;/li&gt;
  5. &lt;/ul&gt;
  6. {% if current_user.is_authenticated %}
  7. &lt;ul&gt;
  8. &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Logout&lt;/a&gt;&lt;/li&gt;
  9. &lt;/ul&gt;
  10. {% else %}
  11. &lt;ul&gt;
  12. &lt;li&gt;&lt;a href=&quot;{{ url_for(&#39;signup&#39;) }}&quot;&gt;Sign up&lt;/a&gt;&lt;/li&gt;
  13. &lt;li&gt;&lt;a href=&quot;{{ url_for(&#39;login&#39;) }}&quot;&gt;Login&lt;/a&gt;&lt;/li&gt;
  14. &lt;/ul&gt;
  15. {% endif %}
  16. &lt;/nav&gt;

login.html

  1. {% extends &#39;base.html&#39; %}
  2. {% block title %}
  3. Login
  4. {% endblock %}
  5. {% block content %}
  6. &lt;div class=&quot;container&quot;&gt;
  7. &lt;form method=&quot;POST&quot; class=&quot;form-register&quot; style=&quot;text-align: center;&quot;&gt;
  8. {{ form.hidden_tag() }}
  9. {{ form.csrf_token }}
  10. {{ form.email.label() }}
  11. {{ form.email(class=&#39;form-control&#39;,placeholder=&#39;example@example.com&#39;) }}
  12. &lt;br&gt;
  13. {{ form.password.label() }}
  14. {{ form.password(class=&#39;form-control&#39;,placeholder=&#39;Password&#39;) }}
  15. &lt;br&gt;
  16. {{ form.submit(class=&#39;btn btn-lg btn-block btn-primary&#39;) }}
  17. &lt;/form&gt;
  18. &lt;/div&gt;
  19. {% endblock %}

Please note that I called my model Account not User

Edit:

Here is models.py:

  1. from market import db
  2. from market import bcrypt,login_manager
  3. from flask_login import UserMixin
  4. @login_manager.user_loader
  5. def load_user(user_id):
  6. return Account.query.filter_by(id=user_id)
  7. class Account(db.Model,UserMixin):
  8. id = db.Column(db.Integer,primary_key = True)
  9. first_name = db.Column(db.String(length = 50),nullable = False)
  10. last_name = db.Column(db.String(length = 50),nullable =False)
  11. email = db.Column(db.String(length = 100),nullable =False,unique = True)
  12. password_hashed = db.Column(db.String(length = 25),nullable = False)
  13. #country = db.Column(db.String,nullable = False)
  14. items = db.relationship(&#39;Item&#39;,backref = &#39;owner&#39;,lazy = True)
  15. @property
  16. def password(self):
  17. return self.password
  18. @password.setter
  19. def password(self,plain_text_password):
  20. self.password_hashed = bcrypt.generate_password_hash(plain_text_password).decode(&#39;utf-8&#39;)
  21. def check_password(self,attempted_password):
  22. 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.

huangapple
  • 本文由 发表于 2023年2月9日 00:20:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388726.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定