我有一个网站。我想让人们可以很容易地更新他们的电子邮件地址,只需在表单中输入它。这是我目前的代码作为一个尝试,但我所尝试的一切都不会奏效。我已经为他们的电子邮件建立了一个专栏,命名为email。
@app.route('/dashboard/settings', methods=['GET', 'POST'])
@login_required
def settings():
form = EmailForm()
if form.validate_on_submit():
current_user.email = form.email.data
session.commit()
return redirect("dashboard/settings")
return render_template("settings.html", form=form)
这些是我的插件
from flask import Flask, render_template, session, abort, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin, login_user, LoginManager, login_required, logout_user, current_user
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import InputRequired, Length, ValidationError
from flask_bcrypt import Bcrypt
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from flask_security import roles_required
问题是我没有找到我想要替换的用户信息。我做了一个查询来查找当前用户行,然后我编辑了电子邮件到表单收到的数据。
def settings():
form = EmailForm()
if form.is_submitted():
user = User.query.filter_by(username=current_user.username).first()
user.email = form.email.data
db.session.commit()
return redirect("/dashboard/settings")
return render_template("settings.html", form=form)