问题描述
我正在我的网站(在 Heroku 上使用 Postgresql 数据库托管)上实现flask-login,我目前正在这里测试注册功能。当我提交表单以注册新用户时,表单将被提交,但数据库不会更新,网站也不会重定向。
这是 routes.py
中 /register
路线的代码:
@application.route('/register',methods=['GET','POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = RegistrationForm()
if form.validate_on_submit():
session = get_session() <--- prints "Session started"
# Don't need to give the id because it is set to auto-increment (serial type) in Postgresql
user = User(username=form.username.data)
print("after username")
user.set_password(form.password.data)
print("after password")
session.add(user)
print("after add")
session.commit()
# print("after commit") <--- commenting out this line breaks the program
return redirect(url_for('login'))
return render_template('register.html',title='Register',form=form)
表单是用flask-wtforms编写的。这是 get_session()
函数,它创建一个 sqlAlchemy 会话,以及在每个请求结束时关闭它的函数(引擎在代码中的其他地方定义)。
def get_session():
if 'session' not in g:
g.session = Session(engine)
print('Session started')
return g.session
@application.teardown_request
def teardown_session(exception):
session = g.pop('session',None)
if session is not None:
session.close()
print('Session torn down')
最后,这里是 models.py
中的 User
类。它基于 Miguel Grinberg 关于 Flask 的教程 Mega-Tutorial。
from app import login
from flask_login import UserMixin
from sqlalchemy import Column,Integer,Text
from sqlalchemy.ext.declarative import declarative_base
from werkzeug.security import generate_password_hash,check_password_hash
# Declare base to create a table as mapped subclass of the base
base = declarative_base()
# The user inherits the functions of base and UserMixin
class User(base,UserMixin):
# Map the class to the Postgresql table 'users'
__tablename__ = 'users'
id = Column('flask-id',primary_key=True)
username = Column('username',Text,nullable=False)
password_hash = Column('password-hash',nullable=False)
# __repr__ function describes the object
def __repr__(self):
return '<User {}>'.format(self.username)
# Create functions for saving and storing hashed passwords using werkzeug
# Assigns the hashed password to the User object (the object can be later committed to the database)
def set_password(self,password):
self.password_hash = generate_password_hash(password)
# Checks the given plaintext password against the hashed one loaded from the User object
def check_password(self,password):
return check_password_hash(self.password_hash,password)
# Flask-login requires a user_loader route to load user objects based on their flask id
# Flask stores the key in memory as a string,so it must be converted to int
@login.user_loader
def load_user(id):
# Trying to avoid circular dependencies
from app.routes import get_session
session = get_session()
return session.query(User).get(int(id))
这是在 Heroku 上运行程序时的控制台输出:
app[web.1]: Session started
app[web.1]: Session torn down
因为打印了“会话已启动”和“会话已拆除”,我知道该表单能够被验证并收到一个 POST 请求。似乎程序在此之后“退出”了该功能。它不会跟踪并最终重定向到 /login
页面,而是刷新 /register
页面。但是,如果我只是添加注释或打印语句,它不会像那样退出,这让我认为这是一个时间问题,执行打印语句/注释所需的额外时间允许数据库进行更改。但是,Python 是同步工作的,所以我不确定这是否是潜在的问题。任何帮助将不胜感激!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)