【问题标题】:Flash a error/success message on flask after .validate_on_submit在 .validate_on_submit 之后在烧瓶上闪烁错误/成功消息
【发布时间】:2020-04-17 20:41:35
【问题描述】:

Flask 的新手和学习做一些体面的事情的时间很短(大学)。 我有一项调查,我想使用 FLASH 来显示成功和错误消息(类别),虽然我无法这样做,但我对其进行了很好的研究,并且确实了解了它的工作原理,我的代码似乎与我找到的示例相比很好,但显然不是。如果有人可以帮助我,将不胜感激。

这是 .py 文件

def index():
    form = Form()
    if form.validate_on_submit():
        flash(u'Thank you for your collaboration!', 'success')
        if not form.validate_on_submit():
            flash(u'Something went wrong!', 'error')
            return redirect(url_for('index'))
        return redirect(url_for('index'))
    #submission_successful = True #or False. you can determine this.
    return render_template('index1.html', form=form) #submission_successful=submission_successful)

.html 文件

<center>
    <form methods='POST'>
        {{ form.csrf_token }}
        {{ form.hidden_tag() }}
        <div style = font-size:18px; font-weight:bold; margin-left:200px; class="form-field"> {{ form.Email.label }} <br> {{ form.Email }}  </div> <br>
        <div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.sex.label }} {{ form.sex }}   </div> <br>
        <div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.age.label }}  {{ form.age }}  </div> <br>
        <div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.Marital.label }}  {{ form.Marital }}  </div> <br>
        <div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.county.label}}  {{ form.county }}  </div> <br>
        <div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.Property.label }}  {{ form.Property }}  </div> <br>
        <div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.PropertyStatus.label }}  {{ form.PropertyStatus }}  </div> <br>
        <div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.Rooms.label }}  {{ form.Rooms }}  </div> <br>
        <div style = font-size:18px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.People.label}} <br> {{ form.People }} </div> <br>
        <div style = font-size:17px; font-weight:bold; margin-left:100px; class="form-field"> {{ form.submit}}  </div>

        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                {% for category, message in messages %}
                {% if category == 'success'%}
                    <div class='alert alert-success mb-3' role='alert' >
                        {{ message }}
                    </div>
                {% endif %}
                {% if category == 'error'%}
                    <div class='alert alert-danger mb-3' role='alert' >
                        {{ message }}
                    </div>
                {% endif %}

                {% endfor %}
            {% endif %}
        {% endwith %}


    </center>
    </form>

【问题讨论】:

    标签: python-3.x flask web-applications visual-studio-code


    【解决方案1】:

    这是我通常为消息闪烁所做的。 https://hastebin.com/behopumivu.cs 对不起,格式不好,我是电话。但是如果你把它放在你的基本模板中,当你“闪烁”时,它总是会在用户当前所在页面的顶部闪烁一条消息。希望这有帮助!

    【讨论】:

      【解决方案2】:

      这是一个使用 Flash 消息的简单模板...为您的案例定制此模板

      应用程序.py
      from flask import Flask, render_template, redirect, url_for,session, flash
      from flask_wtf import FlaskForm
      from wtforms import (StringField, SubmitField)
      
      
      app = Flask(__name__)
      
      app.config['SECRET_KEY'] = 'mysecretkey'
      
      
      class InfoForm(FlaskForm):
          name = StringField('What are you?')
          submit = SubmitField('Submit')
      
      
      @app.route('/', methods=['GET', 'POST'])
      def index():
          # Create instance of the form.
          form = InfoForm()
          # If the form is valid on submission (we'll talk about validation next)
          if form.validate_on_submit():
              # Grab the data from the breed on the form.
      
              session['name'] = form.name.data
              flash(f"You just changed your name to: {session['name']}")
              return redirect(url_for("index"))
      
          return render_template('index.html', form=form)
      
      
      if __name__ == '__main__':
          app.run(debug=True)
      
      
      #index.html
      
      <html>
      <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
      
      </head>
      
      <div class="container">
      
      
        {# get_flashed_messages() is auto sent to the template with the flash() call #}
            {% for mess in get_flashed_messages()  %}
            <div class="alert alert-warning alert-dismissible fade show" role="alert">
              <button type="button" class="close" data-dismiss="alert" aria-label="Close" class="fade close">
                <span aria-hidden="true">&times;</span>
              </button>
              {{mess}}
              </div>
            {% endfor %}
      
      
      <form method="POST">
          {# This hidden_tag is a CSRF security feature. #}
          {{ form.hidden_tag() }}
          {{ form.name.label }} {{ form.name() }}
          {{ form.submit() }}
      </form>
      </div>
      </html>
      

      【讨论】:

      • 伙计,你是一个活生生的救星。我得到了它的设置,我会调整我的代码。谢谢。一旦它起作用,我会将答案标记为正确。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多