Flask-Security bootstraps your application with various views for handling its configured features to get you up and running as quick as possible. However, you’ll probably want to change the way these views look to be more in line with your application’s visual design.
Flask-Security is packaged with a default template for each view it presents to a user. Templates are located within a subfolder named security. The following is a list of view templates:
Overriding these templates is simple:
You can also specify custom template file paths in the configuration.
Each template is passed a template context object that includes the following, including the objects/values that are passed to the template by the main Flask application context processor:
To add more values to the template context you can specify a context processor for all views or a specific view. For example:
security = Security(app, user_datastore)
# This processor is added to all templates
@security.context_processor
def security_context_processor():
return dict(hello="world")
# This processor is added to only the register view
@security.register_context_processor
def security_register_processor():
return dict(something="else")
The following is a list of all the available context processor decorators:
All forms can be overridden. For each form used, you can specify a replacement class. This allows you to add extra fields to the register form or override validators:
from flask_security.forms import RegisterForm
class ExtendedRegisterForm(RegisterForm):
first_name = TextField('First Name', [Required()])
last_name = TextField('Last Name', [Required()])
security = Security(app, user_datastore,
register_form=ExtendedRegisterForm)
For the register_form and confirm_register_form, each field is passed to the user model (as kwargs) when a user is created. In the above case, the first_name and last_name fields are passed directly to the model, so the model should look like:
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))
first_name = db.Column(db.String(255))
last_name = db.Column(db.String(255))
The following is a list of all the available form overrides:
Flask-Security is also packaged with a default tempalte for each email that it may send. Templates are located within the subfolder named security/mail. The following is a list of email templates:
Overriding these templates is simple:
Each template is passed a template context object that includes values for any links that are required in the email. If you require more values in the templates you can specify an email context processor with the email_context_processor decorator. For example:
security = Security(app, user_datastore)
# This processor is added to all emails
@security.email_context_processor
def security_mail_processor():
return dict(hello="world")