Python tp如何使用flask中的重定向将一些字符串从一个url传递到另一个url
我有一个@app.route“/”,它可以接受url,比如'http://0.0.0.0:5000/valid_data“或者”http://0.0.0.0:5000/invalid_data' . 我有一个@app.route'/update_data',methods=['GET','POST'],它处理GET和set请求 我想通过update_data->Post request传递一些字符串,以使用重定向调用选择_data-get request。这是我的密码Python tp如何使用flask中的重定向将一些字符串从一个url传递到另一个url,python,redirect,flask,url-routing,Python,Redirect,Flask,Url Routing,我有一个@app.route“/”,它可以接受url,比如'http://0.0.0.0:5000/valid_data“或者”http://0.0.0.0:5000/invalid_data' . 我有一个@app.route'/update_data',methods=['GET','POST'],它处理GET和set请求 我想通过update_data->Post request传递一些字符串,以使用重定向调用选择_data-get request。这是我的密码 @app.route('/
@app.route('/<data_select>')
def select_data(data_select):
form = data()
return render_template('data.html', form= form)
@app.route('/update_data', methods=['GET','POST'])
def update_data():
form = update_data()
if request.method == 'GET':
return render_template('update_data.html',form=form)
if request.method == 'POST':
messages = "want to pass this string to select_data()"
return redirect(url_for('valid_data', messages = messages))
请给我一些想法在烧瓶中呈现消息,使用 文档中的示例比我所能解释的更好:
from flask import Flask, flash, redirect, render_template, \
request, url_for
app = Flask(__name__)
app.secret_key = 'some_secret'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != 'admin' or \
request.form['password'] != 'secret':
error = 'Invalid credentials'
else:
flash('You were successfully logged in')
return redirect(url_for('index'))
return render_template('login.html', error=error)
if __name__ == "__main__":
app.run()
确保您设置了密钥app.secret\u key,因为flash使用会话,并且需要密钥来加密Cookie
在模板中:
<!doctype html>
<title>My Application</title>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% block body %}{% endblock %}
请修复缩进并确认这是否是您正在使用的实际代码。这行对吗?form=update\u data?@Burhan Khalid,是的,这就是我正在使用的代码`form=update\u data'是在另一个.py文件中定义的,form=update\u data与您的方法同名,这是非常错误的,特别是如果它是在另一个.py文件中定义的。是否可以在重定向内传递任何字符串,例如返回“index”的重定向URL\u,data=data????如果你没有在重定向中传递它,你可以在之前设置它,然后在以后处理它-请查看示例并阅读文档,如果你不理解它,请问一个特定的问题。我理解flash消息的用法。谢谢你: