Search code examples
flaskflask-security

Why data from input does not get into the database (flask-security)


I use flask-security and wanted to add additional fields to the registration page.
I tried to do as it is written in the documentation

The fields were added, but the data from them is not sent to the database. I also learned that adding new fields is not possible if app. config ['SECURITY_CONFIRMABLE'] = True. I have no errors in the terminal. After sending the email, I get the following.

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 -

What am I doing wrong? Why data from input does not get into the database?

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" %}

Solution

  • First - and I know this is confusing - if CONFIRMABLE=True you need to base your form on the ConfirmRegisterForm(). As for data not getting in to DB - your form field names and DB column names must match! so first_name and last_name should be your DB column names...