flask编写html显示request,html - Python Flask POST/GET request - Stack Overflow

博主正在使用Python Flask、SQLite3和DBBrowser for SQLite构建网站的用户认证系统,但在实现登录功能时遇到问题。首先遇到404错误,将登录表单的action从'login.html'改为'login'后,问题变为400 Bad Request错误。进一步调试后,现在出现'ProgrammingError',提示SQLite对象在不同线程中使用。问题出在尝试在新线程中使用在旧线程中创建的SQLite连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I need help with user authentication in my website. I am using python flask, sqlite3 and DB Browser for SQLite. I'm having trouble with the login part. Each time I try to login and redirect to an html website it says error 404.

Here is the code for the login page:

PackageDrop Login

Username:

Password:

The page that is forwarded to is just simple html, nothing special so I am not uploading it.

If you feel like you need more information to understand, ask and I will post quickly.

Thanks for all the help I get.

EDIT1:

Changed in the login.html the form action from "login.html" to "login" and now it gives a 400 Bad Request error.

EDIT2:

The redirection works. I have changed the code for the login but it makes this error: "ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 7812 and this is thread id 7388". The only thing that I did is just connect to the database like this:

conn = sqlite3.connect('database.db')

This is the new code for the login:

@app.route('/login', methods=['GET', 'POST'])

def login():

error = None

checkP = conn.execute('select pword from users where uname = request.form[\'username\']')

arrayCheck = checkP.fetchall()

if request.method == 'POST':

if request.form['password'] != arrayCheck[[0][0]]:

error = 'Incorrect password'

return redirect(url_for('login.html'))

elif len(arrayCheck) == 0:

error = 'Username or password is incorrect'

return redirect(url_for('login.html'))

else:

session['logged_in'] = True

flash('You are logged in')

return redirect(url_for("userpage"))

return render_template('login.html')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值