Answer the question
In order to leave comments, you need to log in
Why is the post request not working?
I wrote a simple program to learn sqlalchemy, but when sending a post request, it returns an error. Who more experienced can explain why?
from datetime import date
from flask import Flask, request, flash
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.testing import db
from wtforms_alchemy import ModelForm
app = Flask(__name__)
app.config.update(
DEBUG=True,
SECURE_KEY='This must be secret !',
SQLALCHEMY_DATABASE_URI='sqlite:///test.db',
SQLALCHEMY_TRACK_NOTIFICATION=False,
WTF_CSRF_ENABLE=False
)
db = SQLAlchemy(app)
class GuessBookItem(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
author = db.Column(db.String(5, ))
date_create = db.Column(db.Date, default=date.today())
is_visible = db.Column(db.Boolean, default=True, nullable=False)
def to_dict(self):
return {
'id': self.id,
'author': self.author,
'data_create': self.date_create,
'is_visible': self.is_visible
}
class GuessBookForm(ModelForm):
class Meta:
model = GuessBookItem()
@app.route('/', methods=['GET'])
def main():
return str(GuessBookItem.query.all())
# Somethings wrong in there
@app.route('/create', methods=['POST'])
def create():
print(request.form)
form = GuessBookForm(request.form)
if form.validate():
guess_book = GuessBookItem(**form.data)
db.session.add(guess_book)
db.session.commit()
flash('DataBase update')
return ('valid', 200)
if __name__ == '__main__':
db.create_all()
app.run()
C:\Program Files\Python39\lib\site-packages\flask_sqlalchemy\__init__.py:872: 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(
* Serving Flask app 'My app' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
C:\Program Files\Python39\lib\site-packages\flask_sqlalchemy\__init__.py:872: 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(
* Debugger is active!
* Debugger PIN: 687-502-646
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [05/Nov/2021 20:25:30] "GET / HTTP/1.1" 200 -
ImmutableMultiDict([('author', 'EnotShow')])
127.0.0.1 - - [05/Nov/2021 20:25:58] "POST /create HTTP/1.1" 500 -
Traceback (most recent call last):
File "C:\Program Files\Python39\Lib\site-packages\flask\app.py", line 2091, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Program Files\Python39\Lib\site-packages\flask\app.py", line 2076, in wsgi_app
response = self.handle_exception(e)
File "C:\Program Files\Python39\Lib\site-packages\flask\app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
File "C:\Program Files\Python39\Lib\site-packages\flask\app.py", line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Program Files\Python39\Lib\site-packages\flask\app.py", line 1516, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Program Files\Python39\Lib\site-packages\flask\app.py", line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "D:\Да это же мой код\Homework #12\My app.py", line 58, in create
flash('DataBase update')
File "C:\Program Files\Python39\Lib\site-packages\flask\helpers.py", line 391, in flash
session["_flashes"] = flashes
File "C:\Program Files\Python39\Lib\site-packages\flask\sessions.py", line 97, in _fail
raise RuntimeError(
RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.
RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.
Answer the question
In order to leave comments, you need to log in
It seems to me that the key should be SECRET_KEY, not SECURE_KEY.
However, it is easy to check.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question