Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 3.x 之后在烧瓶上闪烁错误/成功消息。在提交时验证_Python 3.x_Flask_Web Applications_Visual Studio Code - Fatal编程技术网

Python 3.x 之后在烧瓶上闪烁错误/成功消息。在提交时验证

Python 3.x 之后在烧瓶上闪烁错误/成功消息。在提交时验证,python-3.x,flask,web-applications,visual-studio-code,Python 3.x,Flask,Web Applications,Visual Studio Code,新手到烧瓶和很短的时间来学习做一些体面的事情(大学)。 我有一个调查,我想用FLASH来闪现成功和错误消息(类别),虽然我还没能做到,但我做了一个很好的研究,我确实了解了它的工作原理,与我找到的示例相比,我的代码似乎很好,但显然不是。如果有人能帮我,我将不胜感激 这是.py文件 def index(): form = Form() if form.validate_on_submit(): flash(u'Thank you for your collaborat

新手到烧瓶和很短的时间来学习做一些体面的事情(大学)。 我有一个调查,我想用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>

{{form.csrf_token}
{{form.hidden_tag()}}
{{form.Email.label}}
{{form.Email}}
{{form.sex.label}{{form.sex}}
{{form.age.label}{{form.age}}
{{form.mary.label}{{form.mary}}
{{form.country.label}{{form.country}}
{{form.Property.label}{{form.Property}}
{{form.PropertyStatus.label}{{form.PropertyStatus}}
{{form.Rooms.label}{{form.Rooms}}}
{{form.People.label}
{{form.People}}
{{form.submit} {%with messages=get\u flashed\u messages(with\u categories=true)%} {%if消息%} {%对于类别,消息中的消息%} {%if category=='success%} {{message}} {%endif%} {%if category='错误'%} {{message}} {%endif%} {%endfor%} {%endif%} {%endwith%}
这是我通常对消息闪烁所做的。
对不起,格式不好,我在打电话。但是,如果你把它放在你的基本模板中,当你做“flash”时,它总是会在用户当前所在页面的顶部闪烁一条消息。希望这有帮助

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

app.py #index.html

{#get#u flashed_messages()通过flash()调用自动发送到模板}
{%for get_flashed_messages()中的混乱%}
&时代;
{{mess}}
{%endfor%}
{#这个隐藏的#标记是CSRF的安全特性。#}
{{form.hidden_tag()}}
{{form.name.label}{{form.name()}}
{{form.submit()}}

伙计,你是个活生生的储蓄者。我得到了它的设置,我会调整我的代码。谢谢一旦它起作用,我将把答案标记为正确。
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)


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