python - Flask写的注册页面,当注册时,如果填写数据库里有的相同数据,就报错
迷茫
迷茫 2017-04-18 10:21:08
0
5
1403

Flask写的注册页面,当注册时,如果填写数据库里有的相同数据,就报错

想知道如何解决

注册路由代码

注册页面代码

报错

E:\Python\Python35-32\lib\site-packages\pymysql\cursors.py:166: Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 479")
  result = self._query(query)
127.0.0.1 - - [21/Feb/2017 09:17:37] "POST /register HTTP/1.1" 200 -
Debugging middleware caught exception in streamed response at a point where response headers were already sent.
Traceback (most recent call last):
  File "E:\Python\Python35-32\lib\site-packages\flask\app.py", line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File "E:\Python\Python35-32\lib\site-packages\flask\app.py", line 1996, in wsgi_app
    ctx.auto_pop(error)
  File "E:\Python\Python35-32\lib\site-packages\flask\ctx.py", line 387, in auto_pop
    self.pop(exc)
  File "E:\Python\Python35-32\lib\site-packages\flask\ctx.py", line 376, in pop
    app_ctx.pop(exc)
  File "E:\Python\Python35-32\lib\site-packages\flask\ctx.py", line 189, in pop
    self.app.do_teardown_appcontext(exc)
  File "E:\Python\Python35-32\lib\site-packages\flask\app.py", line 1898, in do_teardown_appcontext
    func(exc)
  File "E:\Python\Python35-32\lib\site-packages\flask_sqlalchemy\__init__.py", line 822, in shutdown_session
    self.session.commit()
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\scoping.py", line 157, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\session.py", line 874, in commit
    self.transaction.commit()
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\session.py", line 461, in commit
    self._prepare_impl()
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\session.py", line 441, in _prepare_impl
    self.session.flush()
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\session.py", line 2139, in flush
    self._flush(objects)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\session.py", line 2259, in _flush
    transaction.rollback(_capture_exception=True)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\util\langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\util\compat.py", line 187, in reraise
    raise value
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\session.py", line 2223, in _flush
    flush_context.execute()
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 389, in execute
    rec.execute(self)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 548, in execute
    uow
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\persistence.py", line 181, in save_obj
    mapper, table, insert)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\orm\persistence.py", line 835, in _emit_insert_statements
    execute(statement, params)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\engine\base.py", line 945, in execute
    return meth(self, multiparams, params)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\sql\elements.py", line 263, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\engine\base.py", line 1053, in _execute_clauseelement
    compiled_sql, distilled_params
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\engine\base.py", line 1189, in _execute_context
    context)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\engine\base.py", line 1393, in _handle_dbapi_exception
    exc_info
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\util\compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\util\compat.py", line 186, in reraise
    raise value.with_traceback(tb)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\engine\base.py", line 1182, in _execute_context
    context)
  File "E:\Python\Python35-32\lib\site-packages\sqlalchemy\engine\default.py", line 470, in do_execute
    cursor.execute(statement, parameters)
  File "E:\Python\Python35-32\lib\site-packages\pymysql\cursors.py", line 166, in execute
    result = self._query(query)
  File "E:\Python\Python35-32\lib\site-packages\pymysql\cursors.py", line 322, in _query
    conn.query(q)
  File "E:\Python\Python35-32\lib\site-packages\pymysql\connections.py", line 835, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "E:\Python\Python35-32\lib\site-packages\pymysql\connections.py", line 1019, in _read_query_result
    result.read()
  File "E:\Python\Python35-32\lib\site-packages\pymysql\connections.py", line 1302, in read
    first_packet = self.connection._read_packet()
  File "E:\Python\Python35-32\lib\site-packages\pymysql\connections.py", line 981, in _read_packet
    packet.check_error()
  File "E:\Python\Python35-32\lib\site-packages\pymysql\connections.py", line 393, in check_error
    err.raise_mysql_exception(self._data)
  File "E:\Python\Python35-32\lib\site-packages\pymysql\err.py", line 107, in raise_mysql_exception
    raise errorclass(errno, errval)
sqlalchemy.exc.IntegrityError: (pymysql.err.IntegrityError) (1062, "Duplicate entry '123' for key 'ix_users_username'") [SQL: 'INSERT INTO users (username, password) VALUES (%(username)s, %(password)s)'] [parameters: {'username': '123', 'password': '123'}]
127.0.0.1 - - [21/Feb/2017 09:17:42] "POST /register HTTP/1.1" 200 -
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(5)
黄舟

你应该先检查是否此用户已经注册,没注册再db.session.add()。 如果非要想更新主键或unique的记录,可以试试db.session.merge()。 其实执行的是select+update

PHPzhong

sqlalchemy.exc.IntegrityError:(pymysql.err.IntegrityError)(1062,“键'ix_users_username'的重复条目'123'”)[SQL:'插入用户(用户名,密码)VALUES(%(用户名)s, %(password)s)'] [参数: {'用户名': '123', '密码': '123'}]

mysql是否设置了主键id?

迷茫

ix_users_username不能是重复的,换个username试试

左手右手慢动作

用户名唯一

左手右手慢动作

我在前面加了个查询判断 可以了

@app.route("/register", methods=["GET", "POST"])
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        user = User(
            username=form.username.data,
            password=form.password.data
        )
        if(User.query.filter_by(username=user.username).first()):
            flash("当前用户名已经注册!")
            return render_template("/register.html", form=form)
        else:
            flash("注册成功!")
            db.session.merge(user)
            return render_template("/register.html", form=form)
    return render_template("/register.html", form=form)

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!