问题
Have been working on Flask-user for the user management system, But I couldn't see a way to customise the login page and registration page. I installed flask-user using pip
pip install flask-user
Any help would be appreciated
回答1:
In templates
folder, create a new folder named flask_user
and you can copy the login.html
and register.html
pages used by flask-user
to the newly created folder. Then you can modify them as per your requirements. These files will override the pages used by flask-user
.
回答2:
From official flask-user documentation
You can get a boilerplate (an app example) at https://github.com/lingthio/Flask-User-starter-app
Other resources to learn
1. Flask-mega tutorial
Here are a few resources online that can really help you customizing your user-login and also other topics:
https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins
This tutorial uses openid but explains everything with code examples i.e.
app/views.py
from flask import render_template, flash, redirect, session, url_for, request, g
from flask_login import login_user, logout_user, current_user, login_required
from app import app, db, lm, oid
from .forms import LoginForm
from .models import User
@app.route('/login', methods=['GET', 'POST'])
@oid.loginhandler
def login():
if g.user is not None and g.user.is_authenticated:
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
session['remember_me'] = form.remember_me.data
return oid.try_login(form.openid.data, ask_for=['nickname', 'email'])
return render_template('login.html',
title='Sign In',
form=form,
providers=app.config['OPENID_PROVIDERS'])
2. Exploreflask.com
Complete free step by step guide. Also comes with a full chapter for your problem in the 'users' chapter.
回答3:
Follow the official link
Default flask-user BasicApp has email based authorization.
- We can change into user based authorization
Change the model
class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) active = db.Column('is_active', db.Boolean(), nullable=False, server_default='1') username = db.Column(db.String(100, collation='NOCASE'), nullable=False, unique=True) password = db.Column(db.String(255), nullable=False, server_default='') first_name = db.Column(db.String(100, collation='NOCASE'), nullable=False, server_default='') last_name = db.Column(db.String(100, collation='NOCASE'), nullable=False, server_default='') # Define the relationship to Role via UserRoles roles = db.relationship('Role', secondary='user_roles')
In config change the following lines:
USER_ENABLE_EMAIL = False # Enable email authentication USER_ENABLE_USERNAME = True # Disable username authentication
Insert the Default two users by Username. Replace Existing code by following code.
if not User.query.filter(User.username == 'member').first():
user = User(
username='member',
password=user_manager.hash_password('Password1'),
)
db.session.add(user)
db.session.commit()
# Create 'admin@example.com' user with 'Admin' and 'Agent' roles
if not User.query.filter(User.username == 'admin').first():
user = User(
username='admin',
password=user_manager.hash_password('Password1'),
)
user.roles.append(Role(name='Admin'))
user.roles.append(Role(name='Agent'))
db.session.add(user)
db.session.commit()
来源:https://stackoverflow.com/questions/43201102/flask-customising-flask-user-login