python-3.x - 为什么我的函数重定向而不在 Flask 中传递数据
问题描述
我正在尝试创建烧瓶应用程序,将我的 DNA 转换为 RNA。但我无法使用重定向传递数据。这里有什么问题?
蟒蛇代码:
@app.route('/', methods=['GET','POST'])
def home():
form = DNAForm()
if form.validate_on_submit():
# flash(f'Your dna is {form.dna.data.Upper()}', 'success')
dna = form.dna.data.upper()
rna = dna.maketrans('ACGT','UGCA')
return redirect(url_for('home', rna=rna))
return render_template('index.html', form=form)
html代码:
{% if rna %}
<h4>The RNA is: <span class="text-danger">{{ rna }}</span> </h4>
{% endif %}
解决方案
当您进行重定向时,它将返回:
return render_template('index.html', form=form)
所以rna
不会传给index.html
这是解决方法
@app.route('/', methods=['GET','POST'])
def home():
form = DNAForm()
rna = None
if form.validate_on_submit():
dna = form.dna.data.upper()
rna = dna.maketrans('ACGT','UGCA')
# if you don't want the form to be filled with previous data
form.dna.data = ''
return render_template('index.html', form=form, rna = rna)
现在,当form
被提交和验证时,它将给出rna
一些显示的价值index.html
推荐阅读
- javascript - Backbone js:运行 jasmine-karma 单元测试时出现类型错误
- python - 当使用 zeep 模块点击 SOAP 请求时,得到执行“给定的 SOAPAction None 与操作不匹配”。
- php - 如何在 Laravel 迁移中使用自定义文件名?
- python - 从 python 代码中删除不间断空格
- oauth-2.0 - Oauth2 令牌请求不包含 redirect_uri
- javascript - 有人如何循环 JSON 并操作其值(转换为字符串、设置固定小数并添加千位分隔符)?
- anaconda - Anaconda 不启动 - attributeError: 模块 'six' 没有属性 'add_metaclass'
- reactjs - 反应材料选择中的自定义菜单项和布局
- http - OPLON LBL ADC:如何为虚拟域设置“粘性会话”?
- c# - 参考搜索不考虑包\包名