最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[记录]折腾Flask服务器的flash功能

Flash crifan 1351浏览 0评论

折腾:

[记录]CentOS中继续研究Flask服务器

期间,继续去试试Flask的flash功能

参考:

消息闪现 — Flask 0.10.1 文档

Message Flashing — Flask Documentation (0.11)

试试代码:

(SIPEvents)  SIPEvents cat flash.py
from flask import Flask, flash, redirect, render_template, \
     request, url_for
app = Flask(__name__)
app.secret_key = ‘some_secret’
@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’] != ‘secret’:
            error = ‘Invalid credentials’
        else:
            flash(‘You were successfully logged in’)
            return redirect(url_for(‘index’))
    return render_template(‘login.html’, error=error)
if __name__ == "__main__":
    app.run()
(SIPEvents)  SIPEvents cat templates/index.html 
{% extends "layout.html" %}
{% block body %}
  <h1>Overview</h1>
  <p>Do you want to <a href="{{ url_for(‘login’) }}">log in?</a>
{% endblock %}
(SIPEvents)  SIPEvents cat templates/login.html 
{% extends "layout.html" %}
{% block body %}
  <h1>Login</h1>
  {% if error %}
    <p class=error><strong>Error:</strong> {{ error }}
  {% endif %}
  <form action="" method=post>
    <dl>
      <dt>Username:
      <dd><input type=text name=username value="{{
          request.form.username }}">
      <dt>Password:
      <dd><input type=password name=password>
    </dl>
    <p><input type=submit value=Login>
  </form>
{% endblock %}
(SIPEvents)  SIPEvents cat templates/layout.html 
<!doctype html>
<title>My Application</title>
{% with messages = get_flashed_messages() %}
  {% if messages %}
    <ul class=flashes>
    {% for message in messages %}
      <li>{{ message }}</li>
    {% endfor %}
    </ul>
  {% endif %}
{% endwith %}
{% block body %}{% endblock %}

效果:

http://115.29.173.126:5000/

http://115.29.173.126:5000/login

故意输入错误的密码:

对应的log:

(SIPEvents)  SIPEvents flask run –host=0.0.0.0
 * Serving Flask app "flash"
 * Forcing debug mode on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 353-795-063
121.238.252.151 – – [17/Aug/2016 14:28:29] "GET / HTTP/1.1" 200 –
121.238.252.151 – – [17/Aug/2016 14:28:29] "GET / HTTP/1.1" 200 –
121.238.252.151 – – [17/Aug/2016 14:29:23] "GET /login HTTP/1.1" 200 –
121.238.252.151 – – [17/Aug/2016 14:29:48] "POST /login HTTP/1.1" 302 –
121.238.252.151 – – [17/Aug/2016 14:29:48] "GET / HTTP/1.1" 200 –
121.238.252.151 – – [17/Aug/2016 14:29:59] "GET /login HTTP/1.1" 200 –
121.238.252.151 – – [17/Aug/2016 14:30:01] "POST /login HTTP/1.1" 200 –
121.238.252.151 – – [17/Aug/2016 14:30:21] "POST /login HTTP/1.1" 200 –

然后继续参考:

Message Flashing — Flask Documentation (0.11)

消息闪现 — Flask 0.10.1 文档

试试:

with_categories

然后用代码:

(SIPEvents)  SIPEvents cat flash.py
from flask import Flask, flash, redirect, render_template, \
     request, url_for
app = Flask(__name__)
app.secret_key = ‘some_secret’
@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’] != ‘secret’:
            #error = ‘Invalid credentials’
            flash(u’Invalid password provided’, ‘error’)
        else:
            flash(‘You were successfully logged in’)
            return redirect(url_for(‘index’))
    return render_template(‘login.html’, error=error)
if __name__ == "__main__":
    app.run()
(SIPEvents)  SIPEvents cat templates/layout.html 
<!doctype html>
<title>My Application</title>
{% 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 %}
{% block body %}{% endblock %}

结果显示错误信息时,没有红色:

难道是:

需要自己去给error的category,加上css,设置为红色

-》然后就可以实现,例子里面说的:当分类为error时显示红色背景?

搜:

flask get_flashed_messages example

flask get_flashed_messages error red

然后从网页中显示:

中看到了:

<!doctype html>
<title>My Application</title>
<ul class=flashes>
<li class=’error’>Invalid password provided</li>
</ul>
<h1>Login</h1>
<form action="" method=post>
<dl>
<dt>Username:
<dd><input type=text name=username value="admin">
<dt>Password:
<dd><input type=password name=password>
</dl>
<p><input type=submit value=Login>
</form>

的确是:

<li class=’error’>Invalid password provided</li>

看来估计需要我们自己去添加css去设置红色背景。。。

转载请注明:在路上 » [记录]折腾Flask服务器的flash功能

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
56 queries in 0.111 seconds, using 18.32MB memory