Python Flask&Python中的HTML局部变量(UnboundLocalError)

2024-04-23 05:33:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python Flask来托管一个web服务器,它主要工作。我理解这个问题,但我不知道如何解决它。我知道在显示的代码中,当网站第一次启动时,它呈现模板,变量“CurtainCloseTime”没有赋值。所以我有两个表格在我的网站上,我输入一个时间,窗帘关闭和打开,因此变量名称。变量“CCT”表示CurtainCloseTime,是HTML文件中的变量,因为我希望它显示回web页面。在

下面是Python烧瓶代码:

@app.route('/', methods=['GET','POST'])
def index():
    if request.method == 'POST': #If there is an input from form
        CurtainOpenTime = request.form['CurtainOpenTime'] #Assign CurtainOpenTime variable in python to the html CurtainOpenTime variable.
        CurtainCloseTime = request.form['CurtainCloseTime'] #Assign CurtainCloseTime variable in python to the html CurtainCloseTime variable.

    return render_template('Index.html',  CCT=CurtainCloseTime)

Tags: theto代码informweb网站request
1条回答
网友
1楼 · 发布于 2024-04-23 05:33:18

首先,我认为你的问题有缩进问题。我想你的实际代码是:

@app.route('/', methods=['GET','POST'])
def index():
    if request.method == 'POST': #If there is an input from form
        CurtainOpenTime = request.form['CurtainOpenTime'] # CurtainOpenTime variable in python to the html CurtainOpenTime variable.
        CurtainCloseTime = request.form['CurtainCloseTime'] #Assign CurtainCloseTime variable in python to the html CurtainCloseTime variable.

    return render_template('Index.html',  CCT=CurtainCloseTime)

如果这是您的代码(但我们不能确定),那么如果http方法是'GET',CCT就没有值。必须在“if”语句之外指定它:

^{pr2}$

相关问题 更多 >