Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python Flask应用程序中单击导航栏中特定html页面的链接时注销会话_Python_Redirect_Flask_Sqlite_Stay Logged In - Fatal编程技术网

在Python Flask应用程序中单击导航栏中特定html页面的链接时注销会话

在Python Flask应用程序中单击导航栏中特定html页面的链接时注销会话,python,redirect,flask,sqlite,stay-logged-in,Python,Redirect,Flask,Sqlite,Stay Logged In,我对Python Flask应用程序中的重定向有一个问题,请参见: 作为一名新开发人员,我一直在稳步推进这个应用程序,但最近我在某些方面遇到了麻烦。如果您使用“用户名”和“密码”作为用户名和密码登录应用程序,然后单击“添加成员”按钮,您将看到一切正常,并且如果您在“添加成员”网页上并决定返回“搜索成员”或“主页”,您可以轻松添加成员“页面顶部不按“提交”键,您将退出当前会话。有人能帮我理解它为什么这样做吗?我希望用户在这些转换期间保持登录状态,因为这可能会经常发生。下面是一些源代码: 以下是主页

我对Python Flask应用程序中的重定向有一个问题,请参见: 作为一名新开发人员,我一直在稳步推进这个应用程序,但最近我在某些方面遇到了麻烦。如果您使用“用户名”和“密码”作为用户名和密码登录应用程序,然后单击“添加成员”按钮,您将看到一切正常,并且如果您在“添加成员”网页上并决定返回“搜索成员”或“主页”,您可以轻松添加成员“页面顶部不按“提交”键,您将退出当前会话。有人能帮我理解它为什么这样做吗?我希望用户在这些转换期间保持登录状态,因为这可能会经常发生。下面是一些源代码:

以下是主页链接:

@app.route('/home/<defSess>')
@app.route('/home/', defaults={'defSess': None})
def home(defSess):

if defSess is None:
    session.clear()
    return render_template('home.html')
else:
    defSess = defSess

    with sql.connect(databases) as connection:

        connection.execute("CREATE TABLE IF NOT EXISTS dataBaseNames(name varchar(30) DEFAULT NULL)")

        return render_template('home.html', defSess=defSess)
检查用户是否已登录:

def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
    if 'logged_in' in session:
        return f(*args, **kwargs)
    else:
        flash('Unauthorized, Please login', 'danger')
        return redirect(url_for('login'))
return wrap
如果您需要更多的代码,包括HTML代码,请让我知道,否则任何帮助将不胜感激!此外,我意识到可能有一种更有效的方法来编写这个应用程序,但我没有经验,这是我第一次尝试应用程序。无论如何,先谢谢你

# Add member
@app.route('/add_member/<defSess>', methods=['GET', 'POST'])
def add_member(defSess):
form = MemberForm(request.form)
if request.method == 'POST' and form.validate():

    with sql.connect(defSess) as connection:

        name = form.name.data
        author = defSess
        connection.execute("INSERT INTO members(name, author) 
VALUES(?,?)",[name,author],)

        flash('Member Created', 'success')

        return redirect(url_for('memberSearch', defSess=defSess))

return render_template('add_member.html', form=form)
@app.route('/logout/')
@is_logged_in
def logout():
session.clear
flash('You are now logged out', 'success')
session['logged_in'] = False
return redirect(url_for('login'))
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
    if 'logged_in' in session:
        return f(*args, **kwargs)
    else:
        flash('Unauthorized, Please login', 'danger')
        return redirect(url_for('login'))
return wrap