gpt4 book ai didi

sqlite - 类型错误 : __init__() takes 1 positional argument but 5 were given

转载 作者:行者123 更新时间:2023-12-03 17:39:26 24 4
gpt4 key购买 nike

    from flask import Flask, request, flash, url_for, redirect, render_template
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///students.sqlite3'
app.config['SECRET_KEY'] = "random string"

db = SQLAlchemy(app)

class students(db.Model):
id = db.Column('student_id', db.Integer, primary_key = True)
name = db.Column(db.String(100))
city = db.Column(db.String(50))
addr = db.Column(db.String(200))
pin = db.Column(db.String(10))

def __init__( self, name, city, addr, pin):
self.name = name
self.city = city
self.addr = addr
self.pin = pin

def __str__(self):
return "{} {} {} {}".format(self.name,self.city,self.addr,self.pin)

@app.route('/')
def show_all():
return render_template('show_all.html', students = students.query.all() )

@app.route('/new', methods = ['GET', 'POST'])
def new():
if request.method == 'POST':
if not request.form['name'] or not request.form['city'] or not request.form['addr']:
flash('Please enter all the fields', 'error')
else:
student = students(request.form['name'], request.form['city'],
request.form['addr'], request.form['pin'])

db.session.add(student)
db.session.commit()
flash('Record was successfully added')
return redirect(url_for('show_all'))
return render_template('new.html')

if __name__ == '__main__':
db.create_all()
app.run(debug = True)

**这是使用 flask 创建数据库的简单代码,但我在输入时遇到错误..

我得到一个错误内置类型错误类型错误:__init__() 采用 1 个位置参数,但给出了 5 个

最佳答案

请修复缩进,否则您确实在class students 之外定义了__init__

应该是这样的

class students(db.Model):
# class variables

# indent for members
def __init__(self, name, city, addr, pin):
# some code

否则,没有为类提供 __init__ 方法,该类有一个默认的 __init__(self) 构造函数,仅采用 1 个位置参数

关于sqlite - 类型错误 : __init__() takes 1 positional argument but 5 were given,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48224983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com