Flask flash消息原因会话不可用
使用官方文件中的精确代码:Flask flash消息原因会话不可用,flask,Flask,使用官方文件中的精确代码: @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
@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')
没有flash()
一切正常。但添加时会导致错误,因为没有秘密,会话不可用。以下是一个跟踪:
> 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 -
我读过几篇关于这个错误的帖子,但它们都与Flask会话扩展有关,我不使用它
我还尝试在应用程序中添加app.secret\u key='super secret key'
,但没有成功
我应该使用Flask Session并进行适当的配置,还是有一种方法可以只使用普通Flask flash(),它可以在幕后处理Session本身
应用程序配置:
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()
堆栈跟踪中非常清楚地指出,没有设置密钥。请确保将密钥设置在正确的位置,例如:
app = Flask(__name__)
app.secret_key = 'asrtarstaursdlarsn'
UPLOAD_FOLDER = ..
附加的跟踪表明您尚未添加密钥。可能您稍后添加了密钥,但应用程序未与更新的代码同步 我正在分享一个在Flask中使用普通flash消息的基本示例
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\u 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>
闪光例子
{%with messages=get_flashed_messages()%}
{%if消息%}
{消息%中的消息为%s}
- {{message}}
{%endfor%}
{%endif%}
{%endwith%}
虚拟段落
使用闪存消息(/)进行路由:
没有闪存消息的路由(/另一个):
添加一个密钥可能的副本我没有使用此扩展,正如我在我的question@Dmitry您是否尝试使用
config
(非应用程序属性)设置密钥app.config['SECRET_KEY']=…
No,正如我所认为的,它们应该工作相同。我可以试试吗?@Dmitry是的,它们应该工作相同。无论如何,您需要找到设置不起作用的原因。你使用1配置吗?问题是否会在本地重复?
app = Flask(__name__)
app.config['SECRET_KEY'] = 'anystringthatyoulike'