Flask - 在2个不同的路径中使用相同的变量?

时间:2018-05-24 14:12:51

标签: python flask

我正在开发用户注册系统,一旦点击“注册”,该网站就会显示2个表单:第一个表单是一个典型的用户创建表单,包含用户名,密码等,第二个表单是一个表单个人信息,如年龄,生日等。

因为用户在完成个人资料(个人信息)之前可能会离开,所以我不想实际创建用户,即添加用户名/密码信息。到两个表格都提交到数据库。

在第一种形式(User)中创建了一个register()类的实例,需要将其添加到数据库中,但是这个实例是在提交第一个表单后创建的。

所以,我的问题是,有没有办法将变量从第一页传递到下一页?

以下是代码:

@app.route("/profile-creation/<username>", methods = ['GET', 'POST'])
def profile_creation(username):
    form = ProfileForm()

    if form.validate_on_submit():

        # I would like to use the class "user" created in register() here.
        # The user class would be added and commited to a SQLite database.

        flash(f'You have successfully created your account!', 'success')

        return redirect(url_for('homepage'))

    return render_template('profile-creation.html', title = "Create Your Profile", form = form)


@app.route("/register", methods = ['GET', 'POST'])
def register():
    form = RegistrationForm()

    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')

        user = User(username = form.username.data, first_name = form.first_name.data, last_name = form.last_name.data,
        email = form.email.data, password = hashed_password)

        username = user.username

        flash(f'Please set up your profile to create your account.', 'success')

        return redirect(url_for('profile_creation', username = username))

    return render_template('register.html', title = 'Register', form = form)

基本上,如何将user中创建的register()实例传递到profile_page(username)?我听说有办法用会话变量来做,但据说你应该尽可能地避免这些。没有它,有没有办法做到这一点?谢谢。

1 个答案:

答案 0 :(得分:0)

您可以将/register中的原始用户名/密码存储在flask.session中。然后,当用户完成剩余的设置/profile-creation时,可以将收到的完整输入提交到数据库:

import flask 
app = flask.Flask(__name__)

@app.route("/profile-creation/<username>", methods = ['GET', 'POST'])
def profile_creation(username):
   form = ProfileForm()
   if form.validate_on_submit():
       #get any other input fields here
       conn = sqlite3.connect('dbname.db').cursor()
       conn.execute('INSERT INTO users VALUES (?, ?, ?)', [flask.session['username'], flask.session['email'], flask.session['password'])
       #you can add additional columns to your actual database. The above is just an example
       conn.commit()
       conn.close()
       return redirect(url_for('homepage'))
   return render_template('profile-creation.html', title = "Create Your Profile", form = form)


@app.route("/register", methods = ['GET', 'POST'])
def register():
  form = RegistrationForm()
  if form.validate_on_submit():
    hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
    user = User(username = form.username.data, first_name = form.first_name.data, last_name = form.last_name.data,
    email = form.email.data, password = hashed_password)
    username = user.username
    flask.session['password'] = hashed_password
    flask.session['username'] = user
    flask.session['email'] = email
    flash(f'Please set up your profile to create your account.', 'success')
    return redirect(url_for('profile_creation', username = user))
  return render_template('register.html', title = 'Register', form = form)

flask.session存储在运行flask内置服务器期间在整个应用中传递的任何值。