【发布时间】: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 文件?它在哪里?使用
<link href="{{ url_for('static', filename='YOURCSSFILE.CSS') }}"更多信息:stackoverflow.com/questions/16351826/… -
@mmenschig 是否需要显式导入 CSS 文件?我的项目中没有 CSS 文件,我只是使用 Flask-Admin 附带的 bog 标准模板。
标签: python flask flask-admin flask-security