除非重新启动Flask服务器,为什么不刷新数据库列表?

时间:2019-12-15 09:35:32

标签: python sqlite flask

我有一个非常简单的flask应用程序,它允许我注册用户名,然后在列表中显示这些名称。我可以注册,但是列表将为空。如果我重新启动Flask服务器,则在页面加载时,名称将按原样显示。我尝试关闭/重新打开数据库,调用函数(populateuserlist)刷新列表,并禁用html中的浏览器缓存无济于事。任何帮助将不胜感激!

app.py

from flask import Flask, render_template, url_for, request, render_template_string
from flask_wtf import FlaskForm
from wtforms import StringField, SelectField
from wtforms.validators import InputRequired, length
import sqlite3
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secretkey'
conn = sqlite3.connect('users.db')
c = conn.cursor()
#create users.db if it doesn't exist
try:
    c.execute("""CREATE TABLE users (
            username text UNIQUE
            )""")
    print("users.db created with users table")
except:
    print("users.db file exists")

c.execute("SELECT * FROM users")
userlist=c.fetchall()
conn.close()

#populate userlist from users.db
def populateuserlist():
    conn = sqlite3.connect('users.db')
    c = conn.cursor()
    c.execute("SELECT * FROM users")
    userlist = c.fetchall()
    conn.close()
    return userlist

#form with drop down list for users page
class selectuserform(FlaskForm):
    populateuserlist()
    userlist = populateuserlist()
    print("Login form populated with" + str(userlist) + ".")
    currentuser = SelectField('currentuser', choices=[(user[0], user[0]) for user in userlist])

class RegisterNameForm(FlaskForm):
    RegisterName = StringField('Username', validators= [InputRequired(), length(min=1, max=15)])

#define page routes
@app.route('/login', methods=['GET', 'POST'])
def login_page():
    form = RegisterNameForm()
    if form.validate_on_submit():
        newuser=request.form.get("RegisterName")
        conn = sqlite3.connect('users.db')
        c = conn.cursor()
        c.execute("INSERT OR IGNORE INTO users VALUES (?)", (newuser,))
        conn.commit()
        conn.close()

    form = selectuserform()
    populateuserlist()

    return render_template('login.html', userlist=userlist, form=form)

@app.route('/register', methods=['GET', 'POST'])
def register_page():
    form = RegisterNameForm()
    return render_template('register.html', form=form)

#start flask server
if __name__ == '__main__':
    app.run(debug=True)

login.html

<!<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

</head>
<body>
<div>
    <button onclick="window.location.href = '/register'";>Register</button>
    <center>
        <h1>Select User</h1>
<form method="POST" ACTION="{{ url_for('login_page') }}">
    {{ form.csrf_token }}
    {{ form.currentuser }}
    <br><br>

    <input type="submit" value="Continue">
</form>
    </center>
</div>
</body>
</html>

register.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
    <button onclick="window.location.href = '/login'";>Go back</button>
    <center>
        <h1>Enter your name</h1>
        <form method="POST" action="/login">
        {{ form.hidden_tag() }}
        {{ form.RegisterName }}
            <br><br>
        <input type="submit" value="Create user">
        </form>
    </center>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

您不应该在Web应用程序中使用像这样的全局值,它使您易于接受各种奇怪的行为。此功能:

def populateuserlist():
    conn = sqlite3.connect('users.db')
    c = conn.cursor()
    c.execute("SELECT * FROM users")
    userlist = c.fetchall()
    conn.close()
    return userlist 

对全局userlist名称不执行任何操作。为什么要这样您在本地功能范围内创建一个新名称。我故意跳过global的使用,您不应该尝试使用它。您可以使用以下方法轻松测试范围问题:

a = [1, 2, 3]

def something():
    a = [2, 3, 4]
    return a

something()
print(a)

如此,userlist(全局)在应用启动时仅被评估一次。相反,您需要在视图内调用populateuserlist(应该重新命名,因为它不会填充全局名称)。您的表单中也存在该问题。您需要动态更新选项列表。

@app.route('/login', methods=['GET', 'POST'])
def login_page():
    form = RegisterNameForm()
    if form.validate_on_submit():
        newuser=request.form.get("RegisterName")
        conn = sqlite3.connect('users.db')
        c = conn.cursor()
        c.execute("INSERT OR IGNORE INTO users VALUES (?)", (newuser,))
        conn.commit()
        conn.close()

    form = selectuserform()
    userlist = populateuserlist() # CHANGED HERE
    form.currentuser.choices = [(user[0], user[0]) for user in userlist] # ADD THE NEW OPTIONS (IF ANY)

    return render_template('login.html', userlist=userlist, form=form)