python - render_template 中的可选关键字参数
问题描述
这是我的代码:
@app.route('/', methods=['GET', 'POST'])
def index():
form = CryptoForm()
if session.get('currency'):
currency = session.get('currency')
price1 = Get_info(currency)
price = price1.get_filtered_data()
if form.validate_on_submit():
flash('success', 'success')
currency = form.crypto.data
get_price = Get_info(currency)
session['currency'] = get_price.get_filtered_data()
return redirect(url_for('index'))
return render_template("index.html", form=form, price=price)
我要做的是让用户在表单中输入加密首字母缩写词并提交。当用户提交它时,它应该显示价格。但是当页面第一次加载时没有价格,所以price=price
inrender_template
给了我错误。我怎样才能解决它,只有当他提交表格时才有价格?
解决方案
如果无论是否提交表单,您都想做不同的事情,请检查它是什么method
。
from flask import request
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
# The user submitted something.
else:
# The user fetched something.
推荐阅读
- javascript - 绑定到 AngularJS 中的嵌入
- ms-access - MSAccess 查询更新后不刷新
- google-cloud-platform - Hbase 连接到远程服务器上的 localhost。Zookeper 拒绝连接
- python - 你能突出seaborn分类散点图中的具体观察吗?
- c++ - cpp QT 的访问冲突读取位置
- java - spring boot 项目和 com.microsoft.sqlserver.jdbc.SQLServerException: The "variant" data type is not supported
- c# - Unity Android 应用程序数据库
- c++ - 在 ifstream 中使用 getline() 时出现 EXC_BAD_ACCESS 错误
- javascript - 如何使用 JavaScript 在我的 index.html 文件中嵌入 API 调用?
- sql - 计算每月流失率