【问题标题】:Flask-Security templates not taking effectFlask-Security 模板未生效
【发布时间】:2017-05-27 23:21:05
【问题描述】:

我将 Flask-Security 与我的 Flask-Admin 应用程序一起使用。

当前的 login.html 模板是:

% extends "base.html" %} 
{% from "security/_macros.html" 
import     render_field_with_errors, render_field %} 

{% block content %} 
{% include "security/_messages.html" %}

Custom Login Form

{{ login_form.hidden_tag() }} 
{{ render_field_with_errors(login_form.email) }} 
{{ render_field_with_errors(login_form.password) }} 
{{ render_field_with_errors(login_form.remember) }} 
{{ render_field(login_form.next) }} 
{{ render_field(login_form.submit) }}
{% include "security/_menu.html" %} 
{% endblock %}

这在 /templates/security/login_user.html 中。我将此设置为我的配置中加载的文件。

SECURITY_LOGIN_USER_TEMPLATE = 'security/login_user.html'

如果我将自定义登录表单更改为其他内容,则文本会更改,因此它会正确加载此文件。唯一的问题是它没有样式。它就像 CSS 没有被拾起:

烧瓶管理员_1 | raise value.with_traceback(tb)

烧瓶管理员_1 |顶级模板代码中的文件“/code/app/templates/security/login_user.html”,第 3 行

烧瓶管理员_1 | {% from "security/_macros.html" import render_field_with_errors, render_field %}

烧瓶管理员_1 |顶级模板代码中的文件“/code/app/templates/base.html”,第 26 行

烧瓶管理员_1 | {% 块内容 %}

烧瓶管理员_1 |文件“/code/app/templates/security/login_user.html”,第 9 行,在“内容”块中

烧瓶管理员_1 | {{ login_form.hidden_​​tag() }}

烧瓶管理员_1 |文件“/usr/local/lib/python3.5/dist-packages/jinja2/environment.py”,第 408 行,在 getattr

烧瓶管理员_1 |返回 getattr(obj, 属性)

烧瓶管理员_1 | jinja2.exceptions.UndefinedError: 'login_form' 未定义

base.html*

<html>
<head>
  <title>Flask-Security Example</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
{%- with messages = get_flashed_messages(with_categories=true) -%}
  {% if messages %}
    <ul class=flashes>
    {% for category, message in messages %}
      <li class="{{ category }}">{{ message }}</li>
    {% endfor %}
    </ul>
  {% endif %}
{%- endwith %}
<ul>
{% if current_user.is_authenticated %}
  <li>Hello {{ current_user.email }}</li>
  <li><a href="{{ url_for('security.logout') }}">Logout</a></li>
{% else %}
  <li><a href="{{ url_for('security.login') }}">Login</a></li>
  <li><a href="{{ url_for('security.register') }}">Register</a></li>
{% endif %}
</ul>

{% block content %}
{% endblock %}

</body>
</html>

【问题讨论】:

  • 你在哪里导入 CSS 文件?它在哪里?使用&lt;link href="{{ url_for('static', filename='YOURCSSFILE.CSS') }}" 更多信息:stackoverflow.com/questions/16351826/…
  • @mmenschig 是否需要显式导入 CSS 文件?我的项目中没有 CSS 文件,我只是使用 Flask-Admin 附带的 bog 标准模板。

标签: python flask flask-admin flask-security


【解决方案1】:

Flask-Security 默认会在“/templates/security”中寻找它的模板。如果找不到模板,它将使用默认的捆绑模板。

覆盖默认模板的最简单方法是:

  • Github Source 复制 Flask-Security 模板并将其粘贴到您的文件夹中。你应该有“/templates/security”目录。
  • 根据需要更改模板。
  • 最后,更改“base.html”文件以包含您的 CSS(您甚至可以使用自己的 base.html)。

有了这个,您甚至不需要更改“SECURITY_LOGIN_USER_TEMPLATE”默认配置。

希望对你有帮助。

【讨论】:

    【解决方案2】:

    除非您的 base.html 已经包含 CSS 链接,否则您需要将它们链接到您的登录模板中。 在此处查看此 Flask-Admin 登录示例:

    http://examples.flask-admin.org/auth/admin/login/

    请注意,该页面正在使用 Bootstrap for CSS。您必须明确导入它,否则网站将不知道设计应该是什么:

    将此添加到您的&lt;head&gt;(我推荐您的base.html,这样您就不必一遍又一遍地重复):

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    

    【讨论】:

    • 感谢您的建议。我已经这样做了,现在收到错误 jinja2.exceptions.UndefinedError: 'login_form' is undefined' 我现在开始研究它,但如果你已经知道解决方案,那就太好了!
    • 仅在 css 中链接不应产生该问题。检查你的结束标签,尤其是你刚刚编辑过文档的地方。
    • 请同时显示您的 base.html 和 views.py 文件
    • 我已经添加了我的 base.html - 我没有 views.py!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 2019-10-16
    • 2016-04-22
    • 2018-08-23
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多