[英]AttributeError: 'dict' object has no attribute 'get_reset_password_token'
I have been following the guide at https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-x-email-support to implement a password reset feature in my Flask app.我一直按照https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-x-email-support上的指南在我的 Flask 应用程序中实现密码重置功能。
My error is:我的错误是:
Traceback (most recent call last):
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\David PC\AppData\Local\Programs\Python\Python310\lib\site-packages\flask\app.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "C:\Users\David PC\Desktop\VS Code Python\Flask Site\app.py", line 691, in passwordreset
EmailSender(form.email.data, "Password reset", {"url": url_for("reset_password", token=user.get_reset_password_token(), _external=True)}, 6)
AttributeError: 'dict' object has no attribute 'get_reset_password_token'
My route view is:我的路线视图是:
@app.route('/passwordreset', methods=['GET', 'POST'])
def passwordreset():
if current_user.is_authenticated:
return redirect(url_for('profile'))
form = ResetPasswordRequestForm()
if form.validate_on_submit():
user = mycol.find_one({"email": form.email.data})
if user:
EmailSender(form.email.data, "Password reset", {"url": url_for("reset_password", token=user.get_reset_password_token(), _external=True)}, 6)
return redirect(url_for('homepage'))
flash('If an account with this email was registered please check your email.')
return render_template('reset_password_request.html', title='Reset Password', form=form)
And my User class is:我的用户 class 是:
class User(UserMixin):
def __init__(self, user_json):
self.user_json = user_json
def get_id(self):
object_id = self.user_json.get('_id')
return str(object_id)
def get_reset_password_token(self, expires_in=600):
return jwt.encode(
{'reset_password': self.id, 'exp': time() + expires_in},
app.config['SECRET_KEY'], algorithm='HS256')
@staticmethod
def verify_reset_password_token(token):
try:
id = jwt.decode(token, app.config['SECRET_KEY'],
algorithms=['HS256'])['reset_password']
except:
return
return User.query.get(id)
I have a sneaking suspicion I am missing something basic but I cannot seem to figure it out.我有一个偷偷摸摸的怀疑我错过了一些基本的东西,但我似乎无法弄清楚。 Any help would be appreciated!
任何帮助,将不胜感激!
As noted by @Matiss, I had to wrap my pymongo DB query in my User
class.正如@Matiss 所指出的,我必须将我的 pymongo DB 查询包装在我的
User
class 中。
user = User(mycol.find_one({"email": form.email.data}))
This was the fix to the attribute error.这是对属性错误的修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.