Skip to content

Instantly share code, notes, and snippets.

@jimbrayrcp
Last active August 24, 2023 17:11
Show Gist options
  • Save jimbrayrcp/d9f38174dd68d43229969b4f0c465b56 to your computer and use it in GitHub Desktop.
Save jimbrayrcp/d9f38174dd68d43229969b4f0c465b56 to your computer and use it in GitHub Desktop.
placing line break flash messages ~or~ multi line flash messages in a python flask html template
# EDIT THIS CODE TO YOUR SPECIFIC NEEDS
# THIS CODE REQUIRES SPECIFIC HTML CODE TO BE PLACED
# INSIDE THE TEMPLATE EXAMPLE FOUND BELOW
from flask import Flask, flash
def some_method():
try:
("something that could trigger an exception")
except ("the exception type"):
optional = "some optional variable"
message = f"first sentence \n\n" \
f"second sentence \n\n" \
f" third sentence an optional f string {optional}"
text_msg = message.split('\n')
flash(text_msg, category="warning")
return redirect(url_for("home"))
else:
print("something else")
return redirect(url_for("user")
<!-- PLACED THIS CODE INSIDE YOUR TEMPLATE FILE -->
{% block content %}
<div class="content">
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<p class="{{ category }}">
{% for line in message %}
{{line}}<br>
{% endfor %}
</p>
{% endfor %}
{% endif %}
{% endwith %}
</div>
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment