当用户登录时,在Flask中向用户显示处理结果

2024-10-01 16:40:26 发布

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

我对Flask非常陌生,对Python没有太多经验。 根据我的项目要求,我需要在后端(通过Python脚本)使用用户提供的输入进行某些处理,并在用户登录时向用户显示最终结果。

关于我的项目的一点描述每当用户登录时,他/她以html形式输入任何Twitter句柄,python脚本将获得有关该句柄的某些详细信息(类似用户的数量、关注者、tweet的数量等),此过程大约需要5-6分钟。目前,我正在CSVs中处理和存储所有这些数据,然后用HTML显示结果。你知道吗

现在,我不希望我的用户等待5-6分钟来查看结果,因此,希望处理后的结果存储在数据库中,以便用户可以在登录时查看这些结果,比如说,即使在1-2小时之后。你知道吗

我应该如何使用SQLAlchemy在烧瓶中实现这一点?我已经做了如下登录屏幕的数据库,但不知道如何去做这个。你知道吗

engine = create_engine('sqlite:///tutorial.db', echo=True)
app = Flask(__name__)

@app.route('/')
def home():
    if not session.get('logged_in'):
        return render_template('login.html')
    else:
        return render_template('test.html')

@app.route('/login', methods=['POST'])
def do_admin_login():
    error = False
    POST_USERNAME = str(request.form['username'])
    POST_PASSWORD = str(request.form['password'])
    Session = sessionmaker(bind=engine)
    s = Session()
    query = s.query(User).filter(User.username.in_([POST_USERNAME]), 
User.password.in_([POST_PASSWORD]))

    data = query.first()
    if data:
        session['logged_in'] = True
        flash('Login success')
        return render_template('test.html')
    else:
        flash('Login failed!')
        return render_template('login.html',error=True)

@app.route("/logout")
def logout():
    session['logged_in'] = False
    return home()

那个测试.html上面提到的twitter句柄是用户通过html表单输入的。你知道吗


Tags: 用户intrueappreturnsessiondefhtml

热门问题