为什么来自输入的数据没有进入数据库(flask-security)
Why data from input does not get into the database (flask-security)
我使用 flask-security 并想在注册页面添加额外的字段。
我试着按照 documentation
中的说明去做
字段已添加,但其中的数据未发送到数据库。
我还了解到,如果应用程序无法添加新字段。配置 ['SECURITY_CONFIRMABLE'] = 真。
我在终端中没有错误。发送电子邮件后,我得到以下信息。
reply: b'221 2.0.0 closing connection z17sm242899lfe.135 - gsmtp\r\n'
reply: retcode (221); Msg: b'2.0.0 closing connection z17sm242899lfe.135 - gsmtp'
127.0.0.1 - - [27/Jan/2021 08:49:21] "POST /register HTTP/1.1" 302 -
127.0.0.1 - - [27/Jan/2021 08:49:21] "GET / HTTP/1.1" 200 -
我做错了什么? 为什么输入的数据没有进入数据库?
app.py
app = Flask(__name__)
babel = Babel(app, 'ru')
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:123@localhost/platform'
app.config['SECRET_KEY'] = 'super-secret'
app.config['SECURITY_REGISTERABLE'] = True
app.config['SECURITY_PASSWORD_HASH'] = 'bcrypt'
app.config['SECURITY_PASSWORD_SALT'] = 'salt'
app.config['DEBUG'] = True
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'mail'
app.config['MAIL_PASSWORD'] = 'pass'
app.config['MAIL_USE_SSL'] = True
app.config['SECURITY_EMAIL_SENDER'] = 'mail'
app.config['SECURITY_CONFIRMABLE'] = False
app.config['SECURITY_RECOVERABLE'] = True
app.config['SECURITY_CHANGEABLE'] = True
app.config['SECURITY_REGISTERABLE'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SECURITY_REGISTER_USER_TEMPLATE'] = 'security/register_user.html'
mail = Mail(app)
db = SQLAlchemy(app)
admin = Admin(app)
# models
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
fistName = db.Column(db.String(80))
lastName = db.Column(db.String(80))
roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic'))
class ExtendedRegisterForm(RegisterForm):
first_name = StringField('First Name', [Required()])
last_name = StringField('Last Name', [Required()])
# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore,
register_form=ExtendedRegisterForm)
@app.route('/')
@login_required
def index():
return render_template('index.html')
security/register_user.html
{% from "security/_macros.html" import render_field_with_errors, render_field %}
{% include "security/_messages.html" %}
<h1> Регистрация </h1>
<form action="{{ url_for_security('register') }}" method="POST" name="register_user_form">
{{ register_user_form.hidden_tag() }}
{{ render_field_with_errors(register_user_form.email) }}
{{ render_field_with_errors(register_user_form.password) }}
{% if register_user_form.password_confirm %}
{{ render_field_with_errors(register_user_form.password_confirm) }}
{% endif %}
{{ render_field(register_user_form.first_name) }}
{{ render_field(register_user_form.last_name) }}
{{ render_field(register_user_form.submit) }}
</form>
{% include "security/_menu.html" %}
首先 - 我知道这令人困惑 - 如果 CONFIRMABLE=True,您需要将您的表单基于 ConfirmRegisterForm()。
至于没有进入数据库的数据——您的表单字段名称和数据库列名称必须匹配!所以 first_name 和 last_name 应该是你的数据库列名...
我使用 flask-security 并想在注册页面添加额外的字段。
我试着按照 documentation
字段已添加,但其中的数据未发送到数据库。 我还了解到,如果应用程序无法添加新字段。配置 ['SECURITY_CONFIRMABLE'] = 真。 我在终端中没有错误。发送电子邮件后,我得到以下信息。
reply: b'221 2.0.0 closing connection z17sm242899lfe.135 - gsmtp\r\n'
reply: retcode (221); Msg: b'2.0.0 closing connection z17sm242899lfe.135 - gsmtp'
127.0.0.1 - - [27/Jan/2021 08:49:21] "POST /register HTTP/1.1" 302 -
127.0.0.1 - - [27/Jan/2021 08:49:21] "GET / HTTP/1.1" 200 -
我做错了什么? 为什么输入的数据没有进入数据库?
app.py
app = Flask(__name__)
babel = Babel(app, 'ru')
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:123@localhost/platform'
app.config['SECRET_KEY'] = 'super-secret'
app.config['SECURITY_REGISTERABLE'] = True
app.config['SECURITY_PASSWORD_HASH'] = 'bcrypt'
app.config['SECURITY_PASSWORD_SALT'] = 'salt'
app.config['DEBUG'] = True
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'mail'
app.config['MAIL_PASSWORD'] = 'pass'
app.config['MAIL_USE_SSL'] = True
app.config['SECURITY_EMAIL_SENDER'] = 'mail'
app.config['SECURITY_CONFIRMABLE'] = False
app.config['SECURITY_RECOVERABLE'] = True
app.config['SECURITY_CHANGEABLE'] = True
app.config['SECURITY_REGISTERABLE'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SECURITY_REGISTER_USER_TEMPLATE'] = 'security/register_user.html'
mail = Mail(app)
db = SQLAlchemy(app)
admin = Admin(app)
# models
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
password = db.Column(db.String(255))
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
fistName = db.Column(db.String(80))
lastName = db.Column(db.String(80))
roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic'))
class ExtendedRegisterForm(RegisterForm):
first_name = StringField('First Name', [Required()])
last_name = StringField('Last Name', [Required()])
# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore,
register_form=ExtendedRegisterForm)
@app.route('/')
@login_required
def index():
return render_template('index.html')
security/register_user.html
{% from "security/_macros.html" import render_field_with_errors, render_field %}
{% include "security/_messages.html" %}
<h1> Регистрация </h1>
<form action="{{ url_for_security('register') }}" method="POST" name="register_user_form">
{{ register_user_form.hidden_tag() }}
{{ render_field_with_errors(register_user_form.email) }}
{{ render_field_with_errors(register_user_form.password) }}
{% if register_user_form.password_confirm %}
{{ render_field_with_errors(register_user_form.password_confirm) }}
{% endif %}
{{ render_field(register_user_form.first_name) }}
{{ render_field(register_user_form.last_name) }}
{{ render_field(register_user_form.submit) }}
</form>
{% include "security/_menu.html" %}
首先 - 我知道这令人困惑 - 如果 CONFIRMABLE=True,您需要将您的表单基于 ConfirmRegisterForm()。 至于没有进入数据库的数据——您的表单字段名称和数据库列名称必须匹配!所以 first_name 和 last_name 应该是你的数据库列名...