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

馋奶兔 提交于 2021-02-07 13:31:47

问题


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)

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/61666902/overriding-flask-user-flask-logins-default-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!