Ниже приведен код и ошибка. Не могли бы вы помочь?
Код: Выделить всё
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from send_mail import send_mail
app = Flask(__name__)
ENV = 'dev'
if ENV == 'dev':
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:Password-1@localhost/lexus'
else:
app.debug = False
app.config['SQLALCHEMY_DATABASE_URI'] = ''
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Feedback(db.Model):
__tablename__ = 'feedback'
id = db.Column(db.Integer, primary_key=True)
customer = db.Column(db.String(200), unique=True)
dealer = db.Column(db.String(200))
rating = db.Column(db.Integer)
comments = db.Column(db.Text())
def __init__(self, customer, dealer, rating, comments):
self.customer = customer
self.dealer = dealer
self.rating = rating
self.comments = comments
@app.route('/')
def index():
return render_template('index.html')
@app.route('/submit', methods=['POST'])
def submit():
if request.method == 'POST':
customer = request.form['customer']
dealer = request.form['dealer']
rating = request.form['rating']
comments = request.form['comments']
# print(customer, dealer, rating, comments)
if customer == '' or dealer == '':
return render_template('index.html', message='Please enter required fields')
if db.session.query(Feedback).filter(Feedback.customer == customer).count() == 0:
data = Feedback(customer, dealer, rating, comments)
db.session.add(data)
db.session.commit()
#send_mail(customer, dealer, rating, comments)
return render_template('success.html')
return render_template('index.html', message='You have already submitted feedback')
if __name__ == '__main__':
app.run()
Код: Выделить всё
>>> db.create_all()
Traceback (most recent call last):
File "", line 1, in
File "C:\Users\Haswath G N\.virtualenvs\FEEDBACK-APP-trygjtNU\Lib\site-packages\flask_sqlalchemy\extension.py", line 900, in create_all
self._call_for_binds(bind_key, "create_all")
File "C:\Users\Haswath G N\.virtualenvs\FEEDBACK-APP-trygjtNU\Lib\site-packages\flask_sqlalchemy\extension.py", line 871, in _call_for_binds
engine = self.engines[key]
^^^^^^^^^^^^
File "C:\Users\Haswath G N\.virtualenvs\FEEDBACK-APP-trygjtNU\Lib\site-packages\flask_sqlalchemy\extension.py", line 687, in engines
app = current_app._get_current_object() # type: ignore[attr-defined]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Haswath G N\.virtualenvs\FEEDBACK-APP-trygjtNU\Lib\site-packages\werkzeug\local.py", line 508, in _get_current_object
raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.
>>>
Подробнее здесь: https://stackoverflow.com/questions/783 ... ostgres-db