繁体   English   中英

render_template 中的可选关键字参数

[英]Optional keyword argument in 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)

我要做的是让用户在表单中输入加密首字母缩写词并提交。 当用户提交它时,它应该显示价格。 但是当页面第一次加载时没有价格,所以render_template中的price=price给了我错误。 我怎样才能解决它,只有当他提交表格时才有价格?

如果你想做不同的事情,不管表单是否提交,检查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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM