[英]How to know which button was pressed in flask form? (Flask, HTML)
[英]How to show html text beside a form button with flask
我想在注册表的“提交”按钮旁边显示一个确认注册成功的小文本。 但是,它会在另一个页面上返回我的 html 文本。 我希望它显示在同一页面上,在“按钮”旁边的“表单”内。 我怎么做。 我正在使用python烧瓶。 这是我的代码:
from flask import Flask, render_template, request
from flask_mysqldb import MySQL
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '104041122'
app.config['MYSQL_DB'] = 'PAGINA10'
mysql = MySQL(app)
@app.route('/', methods=['GET', 'POST'])
def form():
if request.method == 'POST':
digitado = request.form
nome = digitado['nome']
cpf = digitado['cpf']
email = digitado['email']
birth = digitado['birth']
cursor = mysql.connection.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS pagina10 (nome VARCHAR(50) NOT NULL, cpf VARCHAR(11) NOT NULL, email VARCHAR(20) NOT NULL, birth DATE NOT NULL)")
cursor.execute("INSERT INTO pagina10 (nome, cpf, email, birth) VALUES (%s, %s, %s, %s)", (nome, cpf, email, birth))
mysql.connection.commit()
cursor.close()
return '<h1> Dados cadastrados com sucesso </h1>'
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
这是我的html页面:
<form method="POST" action="">
<div>
<i class="fa-solid fa-pen"></i>
<input type="text" required name="nome" autofocus placeholder="Nome" data-ls-module="charCounter" maxlength="50" minlength="3"/>
</div>
<div>
<i class="fa-solid fa-id-card"></i>
<input type="text" required name="cpf" autofocus placeholder="CPF" minlength="11" maxlength="11"/>
</div>
<div>
<i class="fa-solid fa-at"></i>
<input type="email" required name="email" autofocus placeholder="E-mail" data-ls-module="charCounter" minlength="5" pattern="[UTF-8]">
</div>
<div>
<i class="fa-solid fa-cake-candles"></i>
<input type="date" required name="birth" autofocus placeholder="Nascimento">
</div>
<button type="submit">Cadastrar</button>
</form>
您可以使用flask
的flash
包在您的网站上显示此类消息。
您的 python 代码可能类似于:
from flask import flash
@app.route('/registrierung', methods=['GET', 'POST'])
@app.route('/Registrierung', methods=['GET', 'POST'])
def registration_page(): # put application's code here
registration_form = RegistrationForm()
# ---------------------------------------------------
if registration_form.validate_on_submit():
print(registration_form.is_submitted())
# ----------------- Database Stuff -------------
flash(f'Account was created!', 'success')
return redirect(url_for('login_page'))
return render_template("registrierung.html", img_var_path=get_background_img_path(), registration_form=registration_form)
你必须在你的 html 代码中使用这样的东西:
<!-- flash messages here -->
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<div class="alert" id="hideMe">
{% for category, message in messages %}
<div class=" alert_{{ category }}">
{{ message }}
</div>
{% endfor %}
</div>
{% endif %}
{% endwith %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.