Python 使用with获取烧瓶中的闪烁消息
我正在检查flask框架中闪烁的消息 这是一个基本示例,其中一个模板文件(Index.html)用于提供初始链接,另一个模板文件(Login.html)创建表单 这些文件是: Login.html:Python 使用with获取烧瓶中的闪烁消息,python,flask,Python,Flask,我正在检查flask框架中闪烁的消息 这是一个基本示例,其中一个模板文件(Index.html)用于提供初始链接,另一个模板文件(Login.html)创建表单 这些文件是: Login.html: <!doctype html> <html> <body> <h1>Login</h1> {% if error %} <p><strong>Error:</str
<!doctype html>
<html>
<body>
<h1>Login</h1>
{% if error %}
<p><strong>Error:</strong> {{ error }}
{% endif %}
<form action = "" method = post>
<dl>
<dt>Username:</dt>
<dd>
<input type = text name = username
value = "{{request.form.username }}">
</dd>
<dt>Password:</dt>
<dd><input type = password name = password></dd>
</dl>
<p><input type = submit value = Login></p>
</form>
</body>
</html>
<!doctype html>
<html>
<head>
<title>Flask Message flashing</title>
</head>
<body>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li<{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<h1>Flask Message Flashing Example</h1>
<p>Do you want to <a href = "{{ url_for('login') }}">
<b>log in?</b></a></p>
</body>
</html>
from flask import Flask, flash, redirect, render_template, request, url_for
app = Flask(__name__)
app.secret_key = 'random string'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login', methods = ['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != 'admin' or \
request.form['password'] != 'admin':
error = 'Invalid username or password. Please try again!'
else:
flash('You were successfully logged in')
return redirect(url_for('index'))
return render_template('login.html', error = error)
if __name__ == "__main__":
app.run(debug = True)
让我困惑的是index.html内部。它使用with messages=get\u flashed\u messages()
从会话中获取消息。我不完全理解它为什么要将与
一起使用?我知道with
用于资源、文件、流等,以控制关闭过程(以及在出现问题时不让某些东西打开等)。在此上下文中,它使用和访问的资源是什么
我尝试删除它(在此上下文中),但出现错误:
jinja2.exceptions.TemplateSyntaxError:遇到未知标记
“消息”
另外,来自的示例用例不将与一起使用,而将与一起使用,因此这里是什么情况?尝试修复index.html
<!doctype html>
<html>
<head>
<title>Flask Message flashing</title>
</head>
<body>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<h1>Flask Message Flashing Example</h1>
<p>Do you want to <a href="{{ url_for('login') }}">
<b>log in?</b></a></p>
</body>
</html>
烧瓶信息闪烁
{%with messages=get_flashed_messages()%}
{%if消息%}
{消息%中的消息为%s}
- {{message}}
{%endfor%}
{%endif%}
{%endwith%}
烧瓶消息闪烁示例
你想去吗
尝试修复index.html
<!doctype html>
<html>
<head>
<title>Flask Message flashing</title>
</head>
<body>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<h1>Flask Message Flashing Example</h1>
<p>Do you want to <a href="{{ url_for('login') }}">
<b>log in?</b></a></p>
</body>
</html>
烧瓶信息闪烁
{%with messages=get_flashed_messages()%}
{%if消息%}
{消息%中的消息为%s}
- {{message}}
{%endfor%}
{%endif%}
{%endwith%}
烧瓶消息闪烁示例
你想去吗
Jinja模板不是Python<模板中的code>with
不是Python上下文管理器,它只是引入了一个新的作用域;此代码定义了一个新变量messages
,该变量只有在结束时才可见
请参阅。Jinja模板不是Python<模板中的code>with
不是Python上下文管理器,它只是引入了一个新的作用域;此代码定义了一个新变量messages
,该变量只有在结束时才可见
参见。因此,不带和的等效方法是将替换为带设置的,并删除endwith
also,不使用和的等效方法是将替换为和set
并删除endwith
请添加更多详细信息。请添加更多详细信息。