Reputation: 3696
I have a little flask app that started to behave strangely recently: the flash messages wouldn't go away (upon moving to a different route). They would appear again on every page.
I learned that helper function that displays the messages: get_flashed_messages is supposed to pop the messages off the session. I tested its functionality with logging and discovered it was working correctly: the '_flash' messages where on the session before it was called and were not on the session object after it was called.
Yet, when I then go to a new route they re-appeared on the session object, and get displayed on the page again.
Thus I've determined that the session object the jinja2 template has access to is a copy of the real one, as it doesn't actually mutate the one I can access in my app.py file (which I can mutate correctly).
Has anyone run into this issue before? I updated my flask and all flask-related packages, but that didn't help.
here's part of my requirements.py file (all the packages that might have an impact).
flask==1.1.2
flask_wtf==0.14.3
flask_cors==3.0.10
jinja2==2.10.3
waitress==1.4.4
Flask-Mobility
secrets==1.0.2
werkzeug==0.16.0
wtforms==2.3.3
And here's the start of my app.py file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import secrets
import requests
from functools import wraps
from bokeh.embed import server_document, server_session
from bokeh.client.session import pull_session
from bokeh.util.session_id import generate_session_id
from flask import Flask, url_for, render_template, redirect, jsonify, send_from_directory, session, request, flash, Markup
from flask_cors import CORS
from flask_mobility import Mobility
from waitress import serve
app = Flask(__name__)
CORS(app)
Mobility(app)
app.config['SECRET_KEY'] = secrets.token_urlsafe(16)
pretty typical setup. I thought this might be an issue with frontend cache or something so I tried it in incognito but it still had the same issue...
of course, the front end isn't anything special either:
<article class="flashes">
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li class="card" onclick=delete_this(this)>
<a title="remove"><p style="margin: 15px; color: #868A8E; cursor: pointer;">
{{message}}
<i class="small material-icons right">close</i>
</p></a>
</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
</article>
Upvotes: 0
Views: 124
Reputation: 3696
Alright I got it. I was saving a Markup object in the session instead of a regular string. when I removed it completely it started working. Don't really know if it was just too large an object or if it was the nature of the object that caused the issue. Either way, that was it.
Upvotes: 1