覆盖Flask-User/Flask-Login的默认模板 [英] 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.
我使用Flask User(我相信它扩展了Flask Login),并且在大多数情况下效果很好.我已经建立了自己全新的登录和注册模板,而不是使用它们提供的模板.在文档之后,我将这些模板放置在 templates/flask_user/login.html
, templates/flask_user/register.html
等.
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.
我还在我的配置文件中设置了 USER_UNAUTHENTICATED_ENDPOINT ="login"
和 USER_UNAUTHORIZED_ENDPOINT ="login"
,其中 login
是我的登录路线.
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.
问题是,如果我注销并尝试直接访问需要用户登录的页面,则会将我发送到 http://localhost:5000/user/sign-
并显示 Flask-Login
登录页面.这有两点错误:1)这不是正确的登录路径,2)这不是正确的登录模板.
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.
来自 __ init __.py
的代码,用于初始化应用程序和LoginManager:
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定义在 models.py
的底部:
UserManager is defined at the bottom of models.py
:
user_manager = UserManager(app, db, User)
推荐答案
我实现了@JBLaf的建议,但这不能解决问题.我意识到呈现给我的默认登录页面实际上不是 flask-login
的 login.html
模板,而是其 login_auth0.html
模板.这是我没有替换为自己的 flask_user
文件夹中的模板,因为我认为没有必要.
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.
两种解决方法:
- 设置
USER_ENABLE_AUTH0 = False
.这使得它使用login.html
模板,这是我自己的自定义模板.但是,这确实给我带来了麻烦,因为我确实允许SSO登录.因此,更好的解决方案是: - 在我的
templates/flask_user
文件夹中创建一个login-auth0.html
文件,该文件仅包含行{%include'flask_user/login.html'%}
.
- Set
USER_ENABLE_AUTH0 = False
. This makes it use thelogin.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: - Create a
login-auth0.html
file in mytemplates/flask_user
folder, containing only the line{% include 'flask_user/login.html' %}
.
现在Flask用户将尝试重定向到auth0模板,该模板将呈现我的 login.html
模板.
Now Flask-User will try to redirect to the auth0 template, which will instead render my login.html
template.
这篇关于覆盖Flask-User/Flask-Login的默认模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!