Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用with获取烧瓶中的闪烁消息_Python_Flask - Fatal编程技术网

Python 使用with获取烧瓶中的闪烁消息

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

我正在检查flask框架中闪烁的消息

这是一个基本示例,其中一个模板文件(Index.html)用于提供初始链接,另一个模板文件(Login.html)创建表单

这些文件是:

Login.html:

<!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
请添加更多详细信息。请添加更多详细信息。