SQL注册表格

时间:2019-03-05 10:45:00

标签: python flask

我们正在使用FLASK创建一个注册页面,但似乎无法弄清楚如何将表单中的数据插入数据库。我们已经阅读了FLASK文档,找不到任何有效的方法。

任何帮助将不胜感激。

代码显示在下面(登录功能已删除):

from flask import Flask, request, render_template, g
import sqlite3

app = Flask(__name__)

DATABASE = "./database.db"

#Database Functions
def get_db():

    print("get_db RUN")

    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)
    return db

@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, '_database', None)

    if db is not None:
        db.close()

def query_db(query, args=(), one=False):
    print("query_db RUN")
    cur = get_db().execute(query, args)
    rv = cur.fetchall()

    print("rv: ", str(rv))
    cur.close()

    return (rv[0] if rv else None) if one else rv

def register_db(username, password, email, date_of_birth):

    print("register_db RUN")

    return query_db("insert into User (username, password, email, date_of_birth) values (?, ?, ?, ?)", [username, password, email, date_of_birth], one=True)

def log_the_user_in(username):
    return render_template("profile.html", username=username)

@app.route("/")
def main_page():
    return render_template("main_page.html")

@app.route("/register", methods=["POST", "GET"])
def register():

    print("register RUN")

    error = None

    if request.method == "POST":

        register_db(request.form["username"], request.form["password"], request.form["email"], request.form["date_of_birth"])

        return render_template("profile.html")

    return render_template("register.html")



if __name__ == "__main__":
    app.run()

0 个答案:

没有答案