烧瓶注册错误:缺少1个位置参数 [英] Flask registration error: missing 1 positional argument

查看:672
本文介绍了烧瓶注册错误:缺少1个位置参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然坚持我的注册表格在Flask。
我得到以下错误msg:

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')

视图,但只在视图结尾(因为它创建了一个循环引用并与我的from pjctBB import app冲突)

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

在我的模型中,我有:

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()

,传递的值通过一个隐藏的字段在窗体本身etcetc和它不工作。
当我删除任何关于排名的意见和模型,我仍然得到相同的错误,但现在变量密码...任何参考...

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!

推荐答案

请尝试更新您的模型,如下所示:

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()

因为你正在调用 User.regUser 方法,不需要 self 参数,但 @staticmethod 装饰器。

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

这篇关于烧瓶注册错误:缺少1个位置参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆