简体   繁体   English

如何在2个不同的app.route中使用Flask Flash消息

[英]How to use flask flash messages in 2 different app.route

I'm fresh developer in flask framework, and I got stuck on these methods for showing error message, and I don't understand how to use flask flash message. 我是Flask框架的新开发人员,我被这些方法用来显示错误消息,因此我不了解如何使用Flask Flash消息。 I was thinking about it for 3 or 4 days and I got some idea how to handle this problem. 我已经考虑了3到4天,我对如何解决这个问题有所了解。 so my plan is simple, I make some authentication login in my viewer. 因此我的计划很简单,我在查看器中进行了一些身份验证登录。 If it results output with value false, it will produce error code and that's error code will be showed in my login page. 如果结果输出为false,它将产生错误代码,并且该错误代码将显示在我的登录页面中。 here is how I've implemented my idea. 这就是我实现我的想法的方式。

@app.route('/login/process', methods = ['POST'])
def loginprocess():
    username = request.form.get('user_name')
    passwd = request.form.get('user_passwd')
    userAdminAuth = userLogin.checkUserAdmin(username, passwd)
    userMemberAuth = userLogin.checkUserMember(username, passwd)
    if userAdminAuth == True and userMemberAuth == False:
      session['logged_in'] = True
      session['username'] = username
      return redirect(url_for('admin'))

    elif userAdminAuth == False and userMemberAuth == True:
      session['logged_in'] = True
      session['username'] = username
      return redirect(url_for('member'))

    else:
      error = 'Invalid username or password'
      return redirect(url_for('login'))

@app.route('/login')
def login():
    return render_template('login.html')

And in the html code I have this one 在html代码中我有一个

{% if error %}
    <div class="alert alert-danger" role="alert">
        <span class="glyphicon glyphicon-exclamation-sign"></span>
        <span class="sr-only">Error</span>
        {{ error }}
    </div>
{% endif %}

The question is how can I pass variable 问题是如何传递变量

error = 'Invalid username or password'

In url route 在网址路由中

@app.route('/login/process', methods=['POST'])

To url route 网址路由

@app.route('/login')

Oh anyway you should know this one 哦,反正你应该知道这个

<form action="/login/process" method="post">
  <div class="form-group">
    <div class="input-group">
      <div class="input-group-addon icon-custumized"><span class="glyphicon glyphicon-user"></span></div>
        <input type="text" name="user_name" class="form-control form-costumized" placeholder="Username">
      </div>
    </div>
    <div class="form-group">
      <div class="input-group">
        <div class="input-group-addon icon-custumized"><span class="glyphicon glyphicon-lock"></span></div>
          <input type="password" name="user_passwd" class="form-control form-costumized" placeholder="Password">
        </div>
      </div>
      <div class="btn-toolbar" role="toolbar" aria-label="action">
        <div class="btn-group" role="group" aria-label="action">
          <a class="btn btn-default btn-customized" href="#"><span class="glyphicon glyphicon-list-alt"></span> <span class="textsize">Register</span></a>
          <button type="submit" class="btn btn-default btn-customized"><span class="textsize">Login</span> <span class="glyphicon glyphicon-menu-right"></span></button>
        </div>
    </div>
</form>

In Python, you have to call: 在Python中,您必须调用:

flask.flash('This is the message')

Then, in the template use get_flashed_messages to get the flashed messages and display them, eg: 然后,在模板中使用get_flashed_messages获取get_flashed_messages的消息并显示它们,例如:

{% with messages = get_flashed_messages(with_categories=True) %}
    {% for category, message in messages %}
        <div class="flash {{category}}">{{message}}</div>
    {% endfor %}
{% endwith%}

Flask documentation has a very simple and nice example. Flask文档中有一个非常简单且不错的示例。

The fact that the message is flashed in one route and displayed in another is not a problem. 消息以一种途径闪烁并以另一种途径显示的事实不是问题。 That is exactly the use case flask.flash was designed for! 那就是用例flask.flash专门用于!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM