2

Use exact code from official docs:

@app.route('/', methods=['GET', 'POST'])
def upload_file():
 if request.method == 'POST':
 ...
 if file.filename == '':
 flash('No selected file')
 return redirect(request.url)
 if file and allowed_file(file.filename):
 ..
 flash('File was successfully uploaded')
 return redirect(url_for('uploaded_file', filename=filename))
 return render_template('home.html')

Without flash() everything works ok. But when adding it causes error The session is unavailable because no secret. Below is a trace:

> 127.0.0.1 - - [18/Feb/2019 10:16:21] "POST / HTTP/1.1" 500 -
> 127.0.0.1 - - [18/Feb/2019 10:17:16] "GET / HTTP/1.1" 200 - [2019年02月18日 10:17:23,999] ERROR in app: Exception on / [POST]
> Traceback (most recent call last): File
> "/home/dvperv/PycharmProjects/astros/venv/lib/python3.6/site-packages/flask/app.py",
> line 2292, in wsgi_app
> response = self.full_dispatch_request() File "/home/dvperv/PycharmProjects/astros/venv/lib/python3.6/site-packages/flask/app.py",
> line 1815, in full_dispatch_request
> rv = self.handle_user_exception(e) File "/home/dvperv/PycharmProjects/astros/venv/lib/python3.6/site-packages/flask/app.py",
> line 1718, in handle_user_exception
> reraise(exc_type, exc_value, tb) File "/home/dvperv/PycharmProjects/astros/venv/lib/python3.6/site-packages/flask/_compat.py",
> line 35, in reraise
> raise value File "/home/dvperv/PycharmProjects/astros/venv/lib/python3.6/site-packages/flask/app.py",
> line 1813, in full_dispatch_request
> rv = self.dispatch_request() File "/home/dvperv/PycharmProjects/astros/venv/lib/python3.6/site-packages/flask/app.py",
> line 1799, in dispatch_request
> return self.view_functions[rule.endpoint](**req.view_args) File "/home/dvperv/PycharmProjects/astros/app.py", line 39, in upload_file
> flash('No file part') File "/home/dvperv/PycharmProjects/astros/venv/lib/python3.6/site-packages/flask/helpers.py",
> line 410, in flash
> session['_flashes'] = flashes File "/home/dvperv/PycharmProjects/astros/venv/lib/python3.6/site-packages/werkzeug/local.py",
> line 350, in __setitem__
> self._get_current_object()[key] = value File "/home/dvperv/PycharmProjects/astros/venv/lib/python3.6/site-packages/flask/sessions.py",
> line 101, in _fail
> raise RuntimeError('The session is unavailable because no secret ' RuntimeError: The session is unavailable because no secret key was
> set. Set the secret_key on the application to something unique and
> secret.
> 127.0.0.1 - - [18/Feb/2019 10:17:24] "POST / HTTP/1.1" 500 -

I have read several posts about this error, but they all relates to a Flask-Session extension, I do not use it.

I also tried to add app.secret_key = 'super secret key' in the application, w/o success.

Shall I use Flask Session and make appropriate configuration or there is a way of just using plain Flask flash() which somehow works with sessions itself behind the scene.

App config:

app = Flask(__name__)
UPLOAD_FOLDER = ..
ALLOWED_EXTENSIONS = ..
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
if __name__ == '__main__':
app.run()
asked Feb 18, 2019 at 7:32
7
  • Possible duplicate of secret key not set in flask session, using the Flask-Session extension Commented Feb 18, 2019 at 8:51
  • 1
    I am not using this extension, as I said in my question Commented Feb 18, 2019 at 8:52
  • @Dmitry Did you try to set secret key using config(not app property)? app.config['SECRET_KEY'] = ... Commented Feb 18, 2019 at 8:54
  • No, as I believe, they should work identically. Shall I try? Commented Feb 18, 2019 at 8:55
  • @Dmitry yes they should work identically. anyway you need to find why setting do not work. Do you use 1 config? Does the problem repeat locally? Commented Feb 18, 2019 at 9:00

3 Answers 3

1

It's being stated very clearly in the stack trace that the secret key is not set. Be sure to set the secret key at the right place, for instance here:

app = Flask(__name__)
app.secret_key = 'asrtarstaursdlarsn'
UPLOAD_FOLDER = ..
answered Feb 18, 2019 at 10:32
Sign up to request clarification or add additional context in comments.

Comments

1

The attached trace indicates that you have not added a secret key. May be you added the secret key later but the application was not synced with updated code.

I am sharing a basic example of using plain flash message in Flask.

app.py:

from flask import Flask, render_template, flash
app = Flask(__name__)
app.secret_key = 'super secret'
@app.route('/')
def index():
 flash("Flashing a dummy message")
 return render_template("flash_example.html")
@app.route('/another')
def show_page_without_flash():
 return render_template("flash_example.html")
if __name__ == '__main__':
 app.run(debug=True)

flash_example.html:

<!DOCTYPE html>
<html>
 <head><title>Flash example</title></head>
 <body>
 {% with messages = get_flashed_messages() %}
 {% if messages %}
 <ul class=flashes>
 {% for message in messages %}
 <li>{{ message }}</li>
 {% endfor %}
 </ul>
 {% endif %}
 {% endwith %}
 <p>Dummy paragraph</p>
 </body>
</html>

Route with a flash message(/):

flash message example

Route without a flash message(/another):

enter image description here

answered Feb 18, 2019 at 10:36

Comments

1
app = Flask(__name__)
app.config['SECRET_KEY'] = 'anystringthatyoulike'

add a secret key

Ali Hashemi
3,4283 gold badges37 silver badges48 bronze badges
answered Jul 11, 2020 at 9:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.