我需要一些帮助。我就是不能让这段代码将数据从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 |
1条答案
按热度按时间rseugnpd1#
您正在错误地创建和使用模型类。
你其实不需要
__init__
在你的情况下。另外,如果您在构造函数中做了一些不正确的事情,那么一个简短的python类复习教程在这里也不会有什么坏处。下面是一个您可以遵循的用户模型模式
你不需要定义
__init__
```class User(db.Model):
"""Users"""
@app.route("/register/", methods=["GET", "POST"])
def register_user():
if request.method == "GET":
return render_template("register.html")