Flask WTF表单未使用sqlite3数据库更新 [英] Flask WTF form not updating with the sqlite3 database

查看:35
本文介绍了Flask WTF表单未使用sqlite3数据库更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RadioField表单项,该表单项使用数据库中其字段的值.

I have a RadioField form item that uses values for its field from the database.

如果未创建数据库,我将无法启动Flask Web服务器,即使它仅在我进入该页面并加载该表单时才应访问数据库.

If the database is not created I cannot start the Flask Webserver even though it should only access the database when I go to that page and load that form.

如果我在Web服务器运行时更​​新数据库,则看不到带有新数据库信息的表单.

If I update the database while the Webserver is running I don't see the form with the new database information.

直到我重新启动Flask Web服务器.

Until I restart the Flask webserver.

如何获取它,以便在您访问页面时强制表单从数据库中重新加载其值.

How can I get it so that it forces the form to reload its values from the database when you visit the page.

我也很确定它将RadioField的值存储在内存中,因为我可以删除数据库,并且Flask Web服务器将继续运行,并且RadioField仍将显示.

Also I am pretty sure it is storing the values of the RadioField in memory since I can delete the database and the Flask Webserver will continue running and the RadioField will still be shown.

我正在使用sqlite3数据库并使用APSW(另一个python sqlite3包装器)对其进行读写

I am using a sqlite3 database and reading and writing to it with APSW(another python sqlite3 wrapper)

这是我的表格

class DatabaseForm(Form):
    listOfRows = db.getDatabase()
    rows = [(str(x[0]), x) for x in listOfRows]
    images = RadioField('images', validators = [Required()], choices = rows)

这是我的观点

@app.route('/database', methods = ['GET', 'POST'])
def database():
    ''' displays the database and allows the user to select an entry '''
    form = DatabaseForm()
    if form.validate_on_submit():

        primaryKey = form.images.data
        primaryKey = int(primaryKey)


        myProgram.start(primaryKey)
        return render_template('simple.html', word = "SUCCESS")
    else:
        print form.errors

    return render_template('database.html', form=form)

推荐答案

您应在视图中设置字段选择:

You should set the field choices in the view:

    form = DatabaseForm()
    form.images.choices = [(str(x[0]), x) for x in listOfRows]
    if form.validate_on_submit():

通过在类上设置一次选项,仅在实例化类时才设置选项,因此它们不会反映在该特定过程的生命周期内所做的任何更改.

By setting them once on the class the options are only set when the class is instantiated, so they wont reflect any changes made during the lifetime of that particular process.

这篇关于Flask WTF表单未使用sqlite3数据库更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆