簡體   English   中英

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

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

我是Flask框架的新開發人員,我被這些方法用來顯示錯誤消息,因此我不了解如何使用Flask Flash消息。 我已經考慮了3到4天,我對如何解決這個問題有所了解。 因此我的計划很簡單,我在查看器中進行了一些身份驗證登錄。 如果結果輸出為false,它將產生錯誤代碼,並且該錯誤代碼將顯示在我的登錄頁面中。 這就是我實現我的想法的方式。

@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')

在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 %}

問題是如何傳遞變量

error = 'Invalid username or password'

在網址路由中

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

網址路由

@app.route('/login')

哦,反正你應該知道這個

<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>

在Python中,您必須調用:

flask.flash('This is the message')

然后,在模板中使用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文檔中有一個非常簡單且不錯的示例。

消息以一種途徑閃爍並以另一種途徑顯示的事實不是問題。 那就是用例flask.flash專門用於!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM