好吧!所以,我正在為我最近在做的一個網站創建一個簡單的登錄程式!我正在使用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)
我運行我的程式,完全沒有錯誤!我打開我的登錄頁面,頁面打開時沒有出現錯誤!這是頁面外觀: 登錄頁面
但是,當我提交表單時,出現錯誤(不是在控制臺中,而是在網頁本身中): 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', '[email protected]');"""
crsr.execute(comm)
User_DATA_connection.commit()
#close data
User_DATA_connection.close()
uj5u.com熱心網友回復:
當您裝飾 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.)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/389886.html
標籤:Python 蟒蛇-3.x sqlite 烧瓶 烧瓶登录
上一篇:在ApacheSSL代理后面嵌入Tomcat的SpringBoot
下一篇:PythonFlask(部署在Heroku上):ImportError:在Heroku上部署時無法從“werkzeug”匯入名稱“secure_filename”
