Flask registration error: missing 1 positional arg

2019-07-08 05:33发布

I am still stuck with my registration form in Flask. I keep getting the following error msg:

TypeError: regUser() missing 1 required positional argument: 'rank'


a@a.com $2a$12$7DG.DR3v3KC6QR6JCa4c4uwH.aONn1yR8vhLEfaGZ6iIihILbvKFW 000000
127.0.0.1 - - [22/Sep/2015 18:49:30] "POST /register HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/alex/Python/pjctbluebook/lib/python3.4/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/alex/Python/pjctbluebook/lib/python3.4/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/alex/Python/pjctbluebook/lib/python3.4/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/alex/Python/pjctbluebook/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/alex/Python/pjctbluebook/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/alex/Python/pjctbluebook/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/alex/Python/pjctbluebook/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/alex/Python/pjctbluebook/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/alex/Python/pjctbluebook/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/alex/Python/pjctbluebook/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/alex/Python/pjctbluebook/pjctBB/views.py", line 52, in register
    User.regUser("aaaa", "aaaa", "aaaaa", "000000")
TypeError: regUser() missing 1 required positional argument: 'rank'

In my views, I have the following (hardcoding "rank" in this case)

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

    elif request.method == 'POST':

        email = request.form['email']
        username = request.form['username']
        password = generate_password_hash(request.form['pass1'])
        rank = 50

        print(email, password, username)
        User.regUser(email, password, username, rank)

    #    db.session.add(User(email, username, password, "50"))
    #    db.session.commit()
        return render_template('index.html')

The class User is imported into Views but only at the end of Views (as it was creating a circular reference and clashing with my "from pjctBB import app")

from pjctBB.models import User

In my models I have:

from pjctBB.views import db


class User(db.Model):

    __tablename__ = "users"

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String, nullable=False)
    username = db.Column(db.String, nullable=False)
    password = db.Column(db.String, nullable=False)
    rank = db.Column(db.Integer, nullable=False)

    def __init__(self, email, username, password, rank):
        self.email = email
        self.username = username
        self.password = password
        self.rank = rank

    def regUser(self, email, username, password, rank):

        db.session.add(User(email, username, password, rank))
        db.session.commit()

I tried to hardcode "rank", pass the value through a hidden field in the form itself etcetc and it doesnt work. when I removed any referebce about rank in both Views and Models, I still get the same error but now for variable "password"...

Please note that when I print the values of email/username/password and rank in the console before trying to pass them to the function: they show accurately in the console.

I am at a loss here. I hope someone can see through the problem and give me some guidance.

Thanks for reading me and I hope to read from you!

2条回答
神经病院院长
2楼-- · 2019-07-08 05:52

In your model you have below line for definition of regUser()

def regUser(self, email, username, password, rank):

which is taking 5 arguments and you are passing 4 while calling it.

Remove the first self from the definition and you will be okey to get going

查看更多
\"骚年 ilove
3楼-- · 2019-07-08 06:08

Please try updating your models as below:

class User(db.Model):

    __tablename__ = "users"

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String, nullable=False)
    username = db.Column(db.String, nullable=False)
    password = db.Column(db.String, nullable=False)
    rank = db.Column(db.Integer, nullable=False)

    def __init__(self, email, username, password, rank):
        self.email = email
        self.username = username
        self.password = password
        self.rank = rank

    @classmethod
    def regUser(cls, email, username, password, rank):
        db.session.add(cls(email, username, password, rank))
        db.session.commit()

since you are calling User.regUser method, no need for self argument there, but @staticmethod decorator.

查看更多
登录 后发表回答