html表单中的数据没有使用flask(python)传递到mysql

de90aj5v  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(383)

我需要一些帮助。我就是不能让这段代码将数据从html表单插入mysql。插入的行始终没有任何数据。完全空了。让我知道你的想法!
ps:mysql还有2个字段,我还没有使用,这些文件都托管在Pythonywhere上。

html(仅表单):

<form action="{{ url_for('register_user') }}" method="post">
    <div class="register-form">

        <input type="text" class="register-field" id="firstname" value="" placeholder="First name" name="firstname">
        <label class="register-field-icon fui-user" for="register-name"></label>

        <input type="text" class="register-field" id="lastname" value="" placeholder="Last name" name="lastname">
        <label class="register-field-icon fui-user" for="register-name"></label>

        <input type="text" class="register-field" id="email" value="" placeholder="Email" name="email">
        <label class="register-field-icon fui-user" for="register-name"></label>

        <input type="password" class="register-field" id="password" value="" placeholder="Password" name="password">
        <label class="register-field-icon fui-lock" for="register-pass"></label>

        <label class="register-field-icon fui-lock" for="register-pass"></label>
        <input type="submit" value="Register" id="register-btn" class="btn register-btn">

    </div>
</form>

flask(没有mysql的连接细节):

from datetime import datetime
from flask import Flask, redirect, render_template, request, url_for, session
from flask_login import current_user, login_required, login_user, LoginManager, logout_user, UserMixin
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import check_password_hash
from flask import session as login_session

@app.route("/register/", methods=["GET", "POST"])
    def register_user():
    if request.method == "GET":
        return render_template("register.html")

    else:

        User_Data = Reg_User(firstname=request.form["firstname"],lastname=request.form["lastname"],email=request.form["email"],password_hash=request.form["password"])#,groups="freeuser")
        db.session.add(User_Data)
        db.session.commit()

        return redirect(url_for('login'))

class Reg_User(db.Model):

    __tablename__ = 'users'

    def __init__(self, firstname, lastname, email, password_hash):#, groups):

        id = db.Column(db.Integer, primary_key=True)
        firstname = db.Column(db.String(20), unique=False, nullable=False)
        lastname = db.Column(db.String(45), unique=False, nullable=False)
        email = db.Column(db.String(45), unique=True, nullable=False)
        password_hash = db.Column(db.String(128), unique=False, nullable=False)

    def __repr__(self):
        return '<Reg_User %r>'.format(self.username)

mysql数据库:

+--------------------------------------------------------------------------

|  id  |  username  |  password_hash  |  firstname  |  lastname  |  email  |   groups  |  regtime  |

+--------------------------------------------------------------------------

|  43  |     NULL     |          NULL           |     NULL      |     NULL     |  NULL  |   NULL   |  2019-01-09 22:00:56 |

|  44  |     NULL     |          NULL           |     NULL      |     NULL     |  NULL  |   NULL   |  2019-01-09 22:55:09 |

|  45  |     NULL     |          NULL           |     NULL      |     NULL     |  NULL  |   NULL   |  2019-01-09 23:04:15 |

|  46  |     NULL     |          NULL           |     NULL      |     NULL     |  NULL  |   NULL   |  2019-01-09 23:10:32 |
vyu0f0g1

vyu0f0g11#

您正在错误地创建和使用模型类。
你其实不需要 __init__ 在你的情况下。另外,如果您在构造函数中做了一些不正确的事情,那么一个简短的python类复习教程在这里也不会有什么坏处。
下面是一个您可以遵循的用户模型模式
你不需要定义 __init__ ```
class User(db.Model):
"""Users"""

__tablename__ = 'users'

id = db.Column(db.Integer, primary_key=True, autoincrement=True)
first_name = db.Column(db.String(25), nullable=False)
last_name = db.Column(db.String(25), nullable=False)
image_url = db.Column(db.Text, default=DEFAULT_IMG_URL)
email = db.Column(db.String(45), unique=True, nullable=False)
password = db.Column(db.String(128), unique=False, nullable=False)

def __repr__(self):
    return f'<User id:{self.id} first_name:{self.first_name} last_name:{self.last_name} image_url:%.8s' % self.image_url

def get_full_name(self):
    return f'{self.first_name} = {self.last_name}'
您可以通过创建模型的示例然后提交到数据库来创建新用户

@app.route("/register/", methods=["GET", "POST"])
def register_user():
if request.method == "GET":
return render_template("register.html")

else:
    first_name = request.form.get('first_name')
    last_name = request.form.get('last_name') 
    email = request.form.get('email')
    password = request.form.get('password')

    # hash your password, this is a separate topic
    hashed = bcrypt.generate_password_hash(password).decode('utf-8')

    user = User(first_name=first_name, last_name=last_name, email=email, password=hashed)

    db.session.add(user)
    db.session.commit()

    return redirect(url_for('login'))

相关问题