Python 在烧瓶中,我如何使用横档?

Python 在烧瓶中,我如何使用横档?,python,flask,Python,Flask,我一直在努力用flask实现用户注册func。这是我完成的代码 import os from flask import Flask, render_template, flash, request, url_for, redirect, session from content_management import Content from dbconnect import connection from wtforms import Form, BooleanField, TextField,

我一直在努力用flask实现用户注册func。这是我完成的代码

import os
from flask import Flask, render_template, flash, request, url_for, redirect, session
from content_management import Content
from dbconnect import connection
from wtforms import Form, BooleanField, TextField, PasswordField, validators
from passlib.handlers.sha2_crypt import sha256_crypt
from MySQLdb import escape_string as thwart
import gc

def register_page():
    try:
        form = RegistrationForm(request.form)
        if request.method == "POST" and form.validate():
            username = form.username.data
            email = form.email.data
            password = sha256_crypt.encrypt((str(form.password.data)))

            c, conn = connection()

            x = c.execute("SELECT * FROM users WHERE username = (%s)", (thwart(username)))
            if int(x) > 0:
                flash("That username is already taken, please choose another")
                return render_template("register.html", form = form)
            else:
                c.execute("INSERT INTO users (username, email, password, tracking) VALUES (%s, %s, %s, %s)", (thwart(username), thwart(password), thwart(email), thwart("/introduction-to-python-programming/")))
                conn.commit()
                flash("Thanks for registering")
                c.close()
                conn.close()
                gc.collect()

                session['login_in'] = True
                session['username'] = username

                return redirect(url_for('dashboard'))
        return render_template("register.html", form = form)

    except Exception as e:
        return(str(e))
当我填写表单并点击submit按钮时,出现如下错误

并非所有参数都在字符串格式化期间转换

我想这是因为阻碍了。 当我插入
print(thwart(username))
时,输出b'username'

x = c.execute("SELECT * FROM users WHERE username = (%s)", (thwart(username)))
但是没有int(x)的值

x = c.execute("SELECT * FROM users WHERE username = (%s)", (thwart(username)))
由于
(thwart(username))
,我不确定上述方法是否有效


您能告诉我如何修复它吗?

要用项目表示元组,右括号前应该有一个尾随逗号:

>>> x = (1)  # without trailing command => `(1) == 1`
>>> type(x)
<type 'int'>
>>> x = (1,)  # with trailing comma
>>> type(x)
<type 'tuple'>
或者您可以使用以下列表:

x = c.execute("SELECT * FROM users WHERE username = (%s)", [thwart(username)])

旁注
根据,未定义返回值。最好使用
cursor.fetch*()
来获取结果。

要用项目表示元组,右括号前应该有一个尾随逗号:

>>> x = (1)  # without trailing command => `(1) == 1`
>>> type(x)
<type 'int'>
>>> x = (1,)  # with trailing comma
>>> type(x)
<type 'tuple'>
或者您可以使用以下列表:

x = c.execute("SELECT * FROM users WHERE username = (%s)", [thwart(username)])

旁注
根据,未定义返回值。您最好使用
cursor.fetch*()
来获取结果。

非常感谢。当我像上面那样修改代码时,它工作得很好。我对你感激不尽。我还是有点模糊。它们之间的区别是什么?@YuiryKozlenko,
(1)
不是一个以
1
作为其项的元组,而是一个
1
。换言之,
(1)==1
@YuiryKozlenko,如果您仍然不清楚tuple literal,请阅读关于感谢一百万的文档。当我像上面那样修改代码时,它工作得很好。我对你感激不尽。我还是有点模糊。它们之间的区别是什么?@YuiryKozlenko,
(1)
不是一个以
1
作为其项的元组,而是一个
1
。换句话说,
(1)==1
@YuiryKozlenko,如果您仍然不清楚tuple literal,请阅读有关的文档