Issue
I'm encountering a RuntimeError in my Flask application, and I'm seeking assistance in resolving the issue. My Python file is named 'auth.py,' and it has a 'database.db' file located nearby.
Here's the error message I'm facing:
File "<stdin>", line 1, in <module>
File "path\to\auth.py", line 10, in <module>
db = SQLAlchemy(app)
...
RuntimeError: Either 'SQLALCHEMY_DATABASE_URI' or 'SQLALCHEMY_BINDS' must be set.
from flask import Flask, render_template, url_for, redirect
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin, login_user, LoginManager, login_required, logout_user, current_user
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import InputRequired, Length, ValidationError
from flask_bcrypt import Bcrypt
app = Flask(__name__)
db = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['SECRET_KEY'] = 'thisisasecretkey'
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), nullable=False, unique=True)
password = db.Column(db.String(80), nullable=False)
@app.route('/')
def home():
return render_template('base.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
return render_template('login.html')
@ app.route('/register', methods=['GET', 'POST'])
def register():
return render_template('register.html')
if __name__ == "__main__":
app.run(debug=True)
I've confirmed that the 'database.db' file is present in the same directory as 'auth.py.' What could be causing this RuntimeError, and how can I resolve it?
Any help or guidance would be greatly appreciated.
Thank you!
Solution
You should add
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['SECRET_KEY'] = 'thisisasecretkey'
Before the db = SQLAlchemy(app)
.
Answered By - zjalicf
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.