【发布时间】:2020-10-10 10:58:11
【问题描述】:
您好,我是一名新程序员,最近开始使用烧瓶制作网站。我最近开始用烧瓶制作一个迷你 YouTube,但我的数据库有这个问题。当我在数据库中创建一个新条目时,它给了我这个错误:“错误:init() 采用 1 个位置参数,但给出了 7 个”
from flask import Flask, redirect, url_for, render_template, session, request
from flask_restful import Api, Resource, reqparse, abort, fields, marshal_with
from flask_sqlalchemy import SQLAlchemy
from app import *
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class ChannelDB(db.Model):
id = db.Column(db.Integer, nullable=True)
name = db.Column(db.String(100), primary_key=True)
email = db.Column(db.String(100), nullable=False)
password = db.Column(db.String(100), nullable=False)
subs = db.Column(db.Integer, nullable=False)
num_of_videos = db.Column(db.Integer, nullable=False)
channel_fields = {
"id": fields.Integer,
"name": fields.String,
"email": fields.String,
"password": fields.String,
"subs": fields.Integer,
"num_of_videos": fields.Integer
}
@app.route("/")
@app.route("/home")
def home():
return render_template("home.html")
@app.route("/login")
def login():
return render_template("login.html")
@app.route("/create_acc", methods=["POST", "GET"])
def create_acc():
if request.method == "POST":
name = request.form["nm"]
check = ChannelDB.query.filter_by(name=name).first()
if not check:
email = request.form["em"]
psw = request.form["ps"]
channel = ChannelDB(0, name, email, psw, 0, 0)
db.session.add(channel)
db.session.commit()
return redirect(url_for("user"))
else:
abort(409, message="Video alredy exists")
if __name__=="__main__":
db.create_all()
app.run(debug=True)
这是我的 .py 文件中的代码
{% extends "base.html" %}
{% block title %}Login Page{% endblock %}
{% block content %}
<form action="/create_acc", method="post">
<p>Name: </p>
<p><input type="text" name="nm"></p>
<p>Email: </p>
<p><input type="text" name="em"></p>
<p>Password: </p>
<p><input type="text" name="ps"></p>
<p>Press this button when you fill the spots above</p>
<p><input type="submit" value="submit"></p>
</form>
{% endblock %}
这是我的 login.html 文件,以防万一。
谁能帮帮我?
【问题讨论】:
-
我看不出您是否在代码中的任何位置定义了 def init。所以也许这就是为什么它为 __init__(constructor) 采用默认的 1 位置参数
标签: python flask flask-sqlalchemy