Flask 和 Python sqlite3 集成错误
Error in Flask and Python sqlite3 Integration
好的!
因此,我正在为我最近开发的一个网站创建一个 简单 登录程序!我正在使用 Flask 来提供我的 HTML 页面模板....
我正在使用 sqlite3 Python 模块以便于使用模块...
因此,我为登录页面的主网站创建了一个 App Route。您可以查看以下代码:
def login():
msg = ''
if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
# Create variables for easy access
username = request.form['username']
password = request.form['password']
#Connection Est.
connection = sqlite3.connect("Users.db")
#cursor
USer_crsr = connection.cursor()
user_cm = """SELECT username, password FROM accounts"""
USer_crsr.execute(user_cm)
USer_result = USer_crsr.fetchall()
if username and password in USer_result:
# Create session data, we can access this data in other routes
session['loggedin'] = True
session['id'] = USer_result['id']
session['username'] = USer_result['username']
# Redirect to home page
return 'Logged in successfully!'
else:
# Account doesnt exist or username/password incorrect
msg = 'Incorrect username/password!'
return render_template("login.html", msg=msg)
我 运行 我的程序完全没有错误!我打开我的登录页面,页面打开时没有出现错误!这是页面外观:
Login Page
但是当我提交我的表单时,我得到一个错误(不是在控制台,而是在网页本身):
405 - Method Not allowed!
此外,这是我用来创建数据库的数据库文件的代码:
#Connection to FILE
User_DATA_connection = sqlite3.connect('Users.db')
crsr = User_DATA_connection.cursor()
#command here
command = """CREATE TABLE IF NOT EXISTS 'accounts' (
'id' int(11) NOT NULL,
'username' varchar(50) NOT NULL,
'password' varchar(250) NOT NULL,
'email' varchar(100) NOT NULL,
PRIMARY KEY ('id')
)
"""
#execute
crsr.execute(command)
comm = """INSERT INTO accounts VALUES (1, 'test', 'test', 'test@gmail.com');"""
crsr.execute(comm)
User_DATA_connection.commit()
#close data
User_DATA_connection.close()
当你装饰你的 Flask 路由函数时,你需要允许 POST 方法。
示例:
@app.route('/login', methods=["GET", "POST"])
def login():
if request.method == "GET":
# Render your page
if request.method == "POST":
# Process the data you POST-ed from your frontend (insert them into the DB, etc.)
好的! 因此,我正在为我最近开发的一个网站创建一个 简单 登录程序!我正在使用 Flask 来提供我的 HTML 页面模板.... 我正在使用 sqlite3 Python 模块以便于使用模块...
因此,我为登录页面的主网站创建了一个 App Route。您可以查看以下代码:
def login():
msg = ''
if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
# Create variables for easy access
username = request.form['username']
password = request.form['password']
#Connection Est.
connection = sqlite3.connect("Users.db")
#cursor
USer_crsr = connection.cursor()
user_cm = """SELECT username, password FROM accounts"""
USer_crsr.execute(user_cm)
USer_result = USer_crsr.fetchall()
if username and password in USer_result:
# Create session data, we can access this data in other routes
session['loggedin'] = True
session['id'] = USer_result['id']
session['username'] = USer_result['username']
# Redirect to home page
return 'Logged in successfully!'
else:
# Account doesnt exist or username/password incorrect
msg = 'Incorrect username/password!'
return render_template("login.html", msg=msg)
我 运行 我的程序完全没有错误!我打开我的登录页面,页面打开时没有出现错误!这是页面外观: Login Page
但是当我提交我的表单时,我得到一个错误(不是在控制台,而是在网页本身): 405 - Method Not allowed!
此外,这是我用来创建数据库的数据库文件的代码:
#Connection to FILE
User_DATA_connection = sqlite3.connect('Users.db')
crsr = User_DATA_connection.cursor()
#command here
command = """CREATE TABLE IF NOT EXISTS 'accounts' (
'id' int(11) NOT NULL,
'username' varchar(50) NOT NULL,
'password' varchar(250) NOT NULL,
'email' varchar(100) NOT NULL,
PRIMARY KEY ('id')
)
"""
#execute
crsr.execute(command)
comm = """INSERT INTO accounts VALUES (1, 'test', 'test', 'test@gmail.com');"""
crsr.execute(comm)
User_DATA_connection.commit()
#close data
User_DATA_connection.close()
当你装饰你的 Flask 路由函数时,你需要允许 POST 方法。 示例:
@app.route('/login', methods=["GET", "POST"])
def login():
if request.method == "GET":
# Render your page
if request.method == "POST":
# Process the data you POST-ed from your frontend (insert them into the DB, etc.)