简体   繁体   English

render_template 中的可选关键字参数

[英]Optional keyword argument in render_template

Here is my code:这是我的代码:

@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)

What I'm trying to do is to get the user to type a crypto acronym into the form and submit it.我要做的是让用户在表单中输入加密首字母缩写词并提交。 When the user submits it, it should display the price.当用户提交它时,它应该显示价格。 But when the page loads for first time there is no price so the price=price in render_template is giving me error.但是当页面第一次加载时没有价格,所以render_template中的price=price给了我错误。 How could I fix it so there is a price only if he submit the form?我怎样才能解决它,只有当他提交表格时才有价格?

If you want to do different things whether the form is submitted or not, check what the method is.如果你想做不同的事情,不管表单是否提交,检查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