您好,我是一名新程序员,我最近开始使用flask创建网站。我最近开始制作带有烧瓶的迷你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文件,如果有帮助的话。
谁能帮我?
__init__
需要关键字参数而不是位置参数。区别在于使用字段名称为值添加前缀:
channel = ChannelDB(id=0, name=name, email=email, password=psw, subs=0, num_of_videos=0)
还有一些提示:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句