Я пытался создать несколько моделей для своей базы данных и подключился с помощью flask -sqlalchemy. Я подключаю свою базу данных из connections.py в app.py и использую его функции, классы из функций в app.py. Все три модели создают базу данных после запуска flask. Мой код app.py
from flask import (Flask, request, redirect, render_template, url_for, session, blueprints)
from flask_sqlalchemy import SQLAlchemy import config from connection import (db, users)
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.sqlite3' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config.from_object(config.DevelopementConfig)
#Link to main Page @app.route('/') def index():
return render_template("index.html")
#Link to Login Page @app.route('/login', method = ["POST", "GET"]) def login():
if request.method == "POST":
user = request.form('username')
password = request.form('password')
#Checking if user exist
check_usr = users.query.filter_by().first()
if check_usr:
session['username'] = check_usr.username
session['password'] = check_usr.password
return redirect(url_for('index'))
else:
#Checking if user is logged in
if "username" in session:
return redirect(url_for('index'))
return render_template('login.html')
#Link to teachers report page @app.route('/report') def report():
return render_template('report.html')
@app.route('/weekly_report') def weekly():
return render_template('weekly.html')
@app.route('/view_weekly_report') def viewW():
return render_template('vieww.html')
@app.route('/view_Daily_report') def viewD():
return render_template('viewd.html')
@app.route('/daily_report') def daily():
return render_template('daily.html')
#Link to children assessment page @app.route('/assessment') def asses():
return render_template('assessment.html')
if __name__ == "__main__":
db.create_all()
app.run()
и это connections.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
#from app import app
from werkzeug.security import generate_password_hash, check_password_hash
app = Flask(__name__)
db = SQLAlchemy(app)
# Database Model for Login Page.
class users(db.Model):
_id = db.Column("id", db.Integer, primary_key=True)
name = db.Column("name", db.String(255))
username = db.Column("username", db.String(100))
email = db.Column("email", db.String(100))
hpassword = db.Column("password", db.String(100))
def set_password(self, password):
self.hpassword = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.hpassword, password)
def __init__(self, name, username, email, hpassword):
self.name = name
self.username = username
self.email = email
#Database Model for Weekly Teachers Report
class wreport(db.Model):
_id = db.Column("id", db.Integer, primary_key=True)
name = db.Column("name", db.String(255))
fdate = db.Column ("from_date", db.String(100))
objective = db.Column("objective", db.String(512))
tplan = db.Column("t_plan", db.String(512))
how = db.Column("how", db.String(512))
material = db.Column("material", db.String(512))
extra = db.Column("extra", db.String(512))
def __init__(self, name, fdate, objective, tplan, how, material, extra):
self.name = name
self.fdate = fdate
self.objective = objective
self.t_plan = t_plan
self.how = how
self.material = material
self.extra = extra
# Database Model for Daily Teachers Report
class dreport(db.Model):
_id = db.Column("id", db.Integer, primary_key=True)
tname = db.Column("teacher_name", db.String(255))
sname = db.Column("student_name", db.String(255))
date = db.Column ("from_date", db.String(100))
topic = db.Column("topic", db.String(512))
pftd = db.Column("plan", db.String(512))
activities = db.Column("activities", db.String(512))
comment = db.Column("comment", db.String(512))
ideas = db.Column("ideas", db.String(512))
def __init__(self, tname, sname, date, topic, pftd, activities, comment, ideas):
self.tname = tname
self.sname = sname
self.date = date
self.topic = topic
self.pftd = pftd
self.activities = activities
self.comment = comment
self.ideas = ideas
Я не понимаю, что пошло не так, но получаю кучу ошибок. Я не могу их понять.
> /home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py:813: UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:". warnings.warn( /home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning. warnings.warn(FSADeprecationWarning( Traceback (most recent call last): File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/bin/flask", line 10, in <module>
sys.exit(main()) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/cli.py", line 967, in main
cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/cli.py", line 586, in main
return super(FlaskGroup, self).main(*args, **kwargs) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/click/core.py", line 1254, in invoke
cmd_name, cmd, args = self.resolve_command(ctx, args) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/click/core.py", line 1297, in resolve_command
cmd = self.get_command(ctx, cmd_name) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/cli.py", line 542, in get_command
rv = info.load_app().cli.get_command(ctx, name) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/cli.py", line 392, in load_app
app = locate_app(self, import_name, None, raise_if_not_found=False) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/app.py", line 20, in <module>
def login(): File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/app.py", line 1315, in decorator
self.add_url_rule(rule, endpoint, f, **options) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/app.py", line 98, in wrapper_func
return f(self, *args, **kwargs) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/app.py", line 1275, in add_url_rule
rule = self.url_rule_class(rule, methods=methods, **options) TypeError: __init__() got an unexpected keyword argument 'method' (env) [sahilmohile@sahil-pc Chili-pili]$ flask start /home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py:813: UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:". warnings.warn( /home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning. warnings.warn(FSADeprecationWarning( Traceback (most recent call last): File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/bin/flask", line 10, in <module>
sys.exit(main()) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/cli.py", line 967, in main
cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/cli.py", line 586, in main
return super(FlaskGroup, self).main(*args, **kwargs) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/click/core.py", line 1254, in invoke
cmd_name, cmd, args = self.resolve_command(ctx, args) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/click/core.py", line 1297, in resolve_command
cmd = self.get_command(ctx, cmd_name) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/cli.py", line 542, in get_command
rv = info.load_app().cli.get_command(ctx, name) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/cli.py", line 392, in load_app
app = locate_app(self, import_name, None, raise_if_not_found=False) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/app.py", line 20, in <module>
def login(): File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/app.py", line 1315, in decorator
self.add_url_rule(rule, endpoint, f, **options) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/app.py", line 98, in wrapper_func
return f(self, *args, **kwargs) File "/home/sahilmohile/Documents/Programming_Projects/Python/Chili-pili/env/lib/python3.8/site-packages/flask/app.py", line 1275, in add_url_rule
rule = self.url_rule_class(rule, methods=methods, **options) TypeError: __init__() got an unexpected keyword argument 'method'
Я попытался отладить и изменить значение для __init__
, но ответа не получил.