[Python] Python flask sqlite - I have a user table but would like to only capture partial info...

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a python app which has a "user" table that stores user info such as name, address, phone, username etc. I have a registration form so that a user can register by just choosing a username, password and email. They can then fill out the rest of their details on a profile page, once signed up.

    So I created a User model to populate the database with all the columns etc. I created a "RegistrationForm" using flaskform. This registration form then uses a view to show the front end to the user.

    My problem is that when I load the registration form in the browser, it looks just as I want. It lets me enter the info, but when I then press "submit", I get an error that basically says:

    File "D:\VS Code Projects\driving-school\myproject\users\views.py", line 31, in register
    user = User(
    ^^^^^^^^^^^^^^^^^^^^^^
    TypeError: __init__() missing 4 required positional arguments: 'lname', 'phone', 'address', and 'postcode'


    Now I know that it's telling me that the code below is the problem area:

    Views.py

    @users.route('/register', methods=['GET','POST'])
    def register():
    form = RegistrationForm()
    if form.validate_on_submit():
    user = User(
    email = form.email.data,
    username = form.username.data,
    password = form.password.data,
    fname = form.fname.data,)

    # Add new owner to database
    db.session.add(user)
    db.session.commit()
    return redirect(url_for('users.login'))
    return render_template('register.html',form=form)


    However, I actually think the error is telling me that a function somewhere is expecting arguments that I don't actually want it to look for or commit to the database.

    So what I can't figure out is quite what is causing it to expect more data fields than the ones on the "registration form". I'm thinking that it has something to do with the database "User model", which I've included below:

    Models.py

    class User(db.Model,UserMixin):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, index=True, nullable=False)
    password_hash = db.Column(db.String(128))
    fname = db.Column(db.String, unique=False, nullable=True)
    lname = db.Column(db.String, unique=False, nullable=True)
    phone = db.Column(db.Integer, unique=True, nullable=True)
    email = db.Column(db.String(64), unique=True, index=True)
    address = db.Column(db.String, unique=False, nullable=True)
    postcode = db.Column(db.String, unique=False, nullable=True)
    profile_image = db.Column(db.String(64), nullable=True, default='default_profile.png')
    role = db.relationship('Role', back_populates='user', lazy=True)
    posts = db.relationship('BlogPost', back_populates='user', lazy=True)

    def __init__(self,email,password, username, fname, lname, phone, address, postcode):
    self.password_hash = generate_password_hash(password)
    self.email = email
    self.username = username
    self.fname = fname
    self.lname = lname
    self.phone = phone
    self.address = address
    self.postcode = postcode


    And I'm kinda thinking it's to do with the "init" part. But I'm just not sure and I've fiddled about with it so much, I'm scared of doing irrepairable damage that I can't fix.

    I'm looking for some kind soul to maybe just point me in the right direction or make the problem a little clearer for me.

    If you want to see the RegistrationForm and Template view, just shout. I have't included them because they don't feel like part of the problem.

    Thanks

    Continue reading...

Compartilhe esta Página