Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

297
Vistas
Overriding Flask-User/Flask-Login's default templates

I have found a similar question here, but the answer does not seem to work for me.

I use Flask User (which extends Flask Login, I believe) and for the most part it works very well. I have built my own completely fresh templates for sign in and registration, rather than using their provided ones. Following the documentation, I have placed these templates at templates/flask_user/login.html, templates/flask_user/register.html and so on.

I have also set USER_UNAUTHENTICATED_ENDPOINT = "login" and USER_UNAUTHORIZED_ENDPOINT = "login" in my config file, where login is the name of my login route.

The problem is that if I'm signed out and I try to directly access a page that requires the user to be logged in, I will be sent to http://localhost:5000/user/sign-in and shown the Flask-Login sign in page. Two things wrong with this: 1) This is not the correct route for signing in, and 2) This is not the correct template for signing in.

I'd be grateful for any help anybody can suggest for this, please.

EDIT:

Code from __init__.py that initialises the app and the LoginManager:

app = Flask(__name__)

@app.context_processor
def inject_production_status():
    return dict(production=True)

# Initialise flask_login
login = LoginManager(app)
login.login_view = 'login'
login.refresh_view = "login"
login.needs_refresh_message = "Please sign in again to access this page"

UserManager is defined at the bottom of models.py:

user_manager = UserManager(app, db, User)
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

UserManager needs to be initialized in __init__.py and not in models.py. UserManager will take care of the initialization of LoginManager.

You need to replace in __init__.py :

# Initialize flask_login
login = UserManager (app, db, User)
login.login_view = 'login'
login.refresh_view = "login"
login.needs_refresh_message = "Please sign in again to access this page"

by

# Initialize flask_user
from .models.user_models import User
user_manager = UserManager (app, db, User)
...
# login is now accessible by user_manager.login_manager

(And remove it from models.py)

Here is an example: https://github.com/lingthio/Flask-User-starter-app/blob/master/app/init.py

Irrelevant to the question but just in case: In the long term it will be easier for you to have a look at app.config.from_object to handle more easily production and development configurations.

over 4 years ago · Santiago Trujillo Denunciar

0

I implemented @JBLaf's suggestion, but this did not resolve the problem. I realised that the default sign in page I was being presented was not actually flask-login's login.html template, but its login_auth0.html template. This was a template that I hadn't replaced in my own flask_user folder, as I didn't think it necessary.

Two ways to resolve this:

  1. Set USER_ENABLE_AUTH0 = False. This makes it use the login.html template, which is my own custom one. However, this caused problems for me down the line because I do allow SSO sign-ins. So the better solution was:
  2. Create a login-auth0.html file in my templates/flask_user folder, containing only the line {% include 'flask_user/login.html' %}.

Now Flask-User will try to redirect to the auth0 template, which will instead render my login.html template.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda