How to flashing a message with link using Flask flash? - python

I'm creating a web app using Flask to deal with GoogleOpenID, these codes are almost finished, except the flashing message contains a link:
#oid.after_login
def create_or_login(resp):
user = db_session.query(User).filter_by(email=resp.email).first()
if user is not None:
flash('Successfully signed in', 'success')
else:
user = User(nickname=resp.fullname, source=GOOGLE, email=resp.email)
db_session.add(user)
db_session.commit()
flash(flashing_message, 'success')
g.user = user
session['nickname'] = user.nickname
return redirect(oid.get_next_url())
It works well when flashing_message is like this: 'Successfully registered, please click here'
But when flashing_message is 'Successfully registered, please click here', it doesn't work (flashes nothing) without throwing any Error. Strangely, sentences between flash() and return doesn't work either (did not set session['nickname] or g.user).

The other answers here focus on changing your template to allow all flash messages to be marked as safe, which may not be what you want.
If you just want to mark certain flashed messages as safe, wrap the text passed to flash() in Markup(). (Flask API Docs for Markup)
For example, instead of:
flash('Successfully registered, please click here')
Wrap the string in Markup() like this:
flash(Markup('Successfully registered, please click here'))
As always, you will need to import Markup from the flask package something like:
from flask import Markup

You need to render a template after calling flash() which should then get the message using get_flashed_messages().
You need to adjust your code to call a template after calling flash(). flash sends the message to next request which can be extracted by the template. The template can look something like:
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message | safe }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
In your view code, I would add a render_template right after the flash() call. Something like:
flash('success')
return render_template('whatever.html')

Escaping HTML is the default behavior, so pass it through the safe filter to display the HTML unescaped:
{{ message|safe }}

I am not sure if it is correct, but the way I solved this was by declaring another variable in the function that was a string of HTML and then passing it through a render_template() function.
And in the template passed it through the safe filter.
For example, (roughly based on) the code you have provided:
#oid.after_login
def create_or_login(resp):
user = db_session.query(User).filter_by(email=resp.email).first()
if user is not None:
flash('Successfully signed in', 'success')
else:
user = User(nickname=resp.fullname, source=GOOGLE, email=resp.email)
db_session.add(user)
db_session.commit()
flash(flashing_message, 'success')
link = "Link" ## ADDED HTML WITH LINK ##
g.user = user
session['nickname'] = user.nickname
return render_template('some_layout.html',
link=link ## PASS THE LINK TO THE TEMPLATE ##
)
Then in the template I added an extra if statement inside the get_flashed_messages() if:
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
... Code that was already here ...
{% if link %} ## PASSED THE LINK TO THE TEMPLATE HERE ##
{{ link | safe }}
{% endif %}
{% endif %}
{% endwith %}

Related

How to render new line from python string to HTML template using FLASK?

message=""
#app.route("/", methods=["GET", "POST"])
def upload_file():
global message
if request.method == "POST":
if request.files:
data=request.files["file"]
if data.filename == "":
message="File doesn't have a name! <br>"
elif allowed_file(data.filename):
message+="File Allowed <br>"
data.save(os.path.join(app.config["FILE_UPLOAD"], data.filename))
message+="File Saved"
if(validate()):
message+="File validated! <br>"
else: message+="Failed validation <br>"
else:
message+="File extension not allowed! <br>"
return render_template("ui.html",message=message)
I'm trying to validate the file uploaded on my ui.html template using flask and I want to send a "message" string back to ui.html about the status of verification and to show it nicely I'm trying to add new line whenever a new string gets added to "message" string so that when I render it in my ui.html, new line is added where I wanted it to be.
This is how I'm rendering the "message" string in ui.html:
{% if message %}
<p>{{ message }}</p>
{% endif %}
But ui.html is not rendering <br> and it is printing it as a string on ui.html template. How can I resolve this? I have tried <br /> as well.
Also mentioned in render html strings in flask templates
flask's template engine (jinja2) assumes that input inside of {{ }} is unsafe and will not allow js or html to be rendered inside of it. The easiest way is to use safe filter in order to do such thing.
{{ message | safe }}
According to flask's documentation https://flask.palletsprojects.com/en/1.1.x/templating/ there are two other ways to control autoescaping behaviour which is either wrap the HTML string in a Markup object or disabling autoescaping altogether like this:
{% autoescape false %}
<p>autoescaping is disabled here
<p>{{ will_not_be_escaped }}
{% endautoescape %}
I tackled it with flash function provided by flask. It prints each message separately so I can add <p> in my HTML File only.
The changes made in ui.html file for rendering are:
{% for message in get_flashed_messages() %}
<p>{{ message }}</p>
{% endfor %}

Flask: Include HTML in a return render_template() or redirect()

I have a login form that checks if the username and password given by the user returns a row or not in the database. If it does, then a session cookie is stored and user is sent back to the index page. If not, the user is sent back to the login page again.
Here's a snippet of the relevant code:
cur.execute("SELECT COUNT(*) FROM users WHERE username = ? AND password = ?", (request.form['username'], request.form['password']))
data = cur.fetchone()[0]
# If given username and password returns a row, create the session
if data == 0:
return redirect(url_for('index'))
else:
session['username'] = request.form['username']
When the user is sent back to the login page again (redirect(url_for('index'))), I want to include a message on the login page saying that the given username or password is invalid.
I'm a bit lost on how to do this exactly.
This is called Message Flashing. The full documentation is here, and it's done by adding the messages on the view side and then rendering them on template like this:
<!doctype html>
<title>My Application</title>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class=flashes>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% block body %}{% endblock %}
Checkout flash at http://flask.pocoo.org/docs/0.10/patterns/flashing/. That should do what you want. Just handle the flash messages in the template for index!

How to display flashing message without reloading the page in Flask?

I'm working on a web application using Flask in Python.
I have small function in my application that calculates some values in the background and displays the result on the web page via a flashing message.
Everything is displaying and working fine but it requires page reloading to get the flashing message.
I want to display messages without reloading page.
I heard that I can do that with js, but I'm not familiar with js.
If you have any ideas or suggestion I would appreciate.
There is my code that could build a better picture of what I'm doing.
This is the renderer between my app and the main html file
{% macro render_field(field) %}
<dt> {{ field.label }}
<dd> {{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</dd>
{% endmacro %}
This is the html file were I want to display flashing messages:
<div class="container-fluid" style="min-height:100%">
{% with messages = get_flashed_messages() %}
{% if messages %}
{% for message in messages %}
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
{{message}}
</div>
{% endfor %}
{% endif %}
{% endwith %}
</div>
Here's what Flask Web Development: Developing Web Applications with Python (pp. 46-48) has to say of Message Flashing:
Sometimes it is useful to give the user a status update after a request is completed. This
could be a confirmation message, a warning, or an error. A typical example is when you
submit a login form to a website with a mistake and the server responds by rendering
the login form again with a message above it that informs you that your username or
password is invalid.
Flask includes this functionality as a core feature. Example 4-6 shows how the flash()
function can be used for this purpose.
Example 4-6. hello.py: Flashed messages
#app.route('/', methods=['GET', 'POST'])
def index():
form = Nameform()
if form.validate_on_submit():
old_name = session.get('name')
if old_name is not None and old_name != form.name.data:
flash('Looks like you have changed your name!')
session['name'] = form.name.data
form.name.data = ''
return redirect(url_for('index'))
return render_template('index.html', form=form, name=session.get('name'))
form = form, name = session.get('name'))
In this example, each time a name is submitted it is compared against the name stored
in the user session, which would have been put there during a previous submission of
the same form. If the two names are different, the flash() function is invoked with a
message to be displayed on the next response sent back to the client.
Calling flash() is not enough to get messages displayed; the templates used by the
application need to render these messages. The best place to render flashed messages is
the base template, because that will enable these messages in all pages. Flask makes a
get_flashed_messages() function available to templates to retrieve the messages and
render them, as shown in Example 4-7.
Example 4-7. templates/base.html: Flash message rendering
{% block content %}
<div class="container">
{% for message in get_flashed_messages() %}
<div class="alert alert-warning">
<button type="button" class="close" data-dismiss="alert">×</button>
{{ message }}
</div>
{% endfor %}
{% block page_content %}{% endblock %}
</div>
{% endblock %}
In this example, messages are rendered using Bootstrap’s alert CSS styles for warning
messages (one is shown in Figure 4-4).
Figure 4-4. Flashed message
A loop is used because there could be multiple messages queued for display, one for
each time flash() was called in the previous request cycle. Messages that are retrieved from get_flashed_messages() will not be returned the next time this function is called,
so flashed messages appear only once and are then discarded.
This is not possible via Python without reloading the page. You must do this in javascript. I suggest CSS styling with display: none and display: block. Here is an example.
1) Python Code, this should go in your app.py or flask.py file.
app.route('/flash/<message>')
def flash(message):
return render_template('flash.html', msg=message)
This will render the HTML page named flash.html. The URL passed in will also have another argument, <message> this is the message that will flash. A URL like this, localhost:80/flash/Hello%20World! will flash the message "Hello World!" on your screen.
There is also another way to pass a message in, this is will arguments. The code for that is like so.
app.route('/flash')
def flash():
message = request.args.get("msg")
return render_template("flash.html", ,msg=message)
This uses the flask's request arguments. So a URL like this, localhost:80/flash?msg=Hello%20World! will give a flashing message saying "Hello World!". If you want to use this method be sure to have the import statement, from flask import request in your import statements.
2) Html Code, this is a separate file named, flash.html in your templates folder.
<body>
<h1 id="header">{{ message }}</h1>
<script>
var heading = $("#header");
setInterval(function() {
if (heading.style.display == "block") { heading.style.display = "none"; }
else if (heading.style.display == "none") { heading.style.display = "block"; }
}, 1000);
</script>
</body>
The 1000 in the setInterval is milliseconds. So the heading will blink every 2 seconds.
You may want to consider using Toastr instead. I ran into the same roadblock with Flask's Flash feature, and Toastr is pure JS. You can use it just like a console log line in your code
toastr.info("Here's a message to briefly show to your user");

display new message to new user App Engine

I'm using Google App engine to allow a user to log in to the site. Once they log in, I create a token for them and use this to check whether the user is logged in. I want to be able to display a different message to first time users and a different one for returning users.
{% ifequal cookie None %}
Log In
<hr></hr>
{% else %}
{% for user in set %}
{% ifequal user.session_ID access_token %}
Logout {{user.user_name}}
<hr></hr>
{% else %}
{%endifequal%}
{% endfor %}
<h3 align="center">
{% endifequal %}
Currently, there are only two choices: signed in and not.
Assuming your user profile entity looks something like this:
class UserProfile(db.Model):
UserID = db.UserProperty()
FirstSession = db.DateTimeProperty(auto_now_add=True)
Try this:
from google.appengine.api import users
user = users.get_current_user()
is_existing_user = UserProfile.all().filter('UserID = ', user).get()
if is_existing_user:
#do something
else:
#do something else
I'd use a boolean variable in the user class for this case:
is_first_time_user = db.BooleanProperty(default=True,verbose_name="is First Time User")
or a function in the user class that the template tag can use.

Custom Django template tag to look up a string username and return it as a user object

I use some third-party template tags in my Django Application (maintained elsewhere) that return a username as a string I can access in my templates like this.
{% for user in gr.user.foll.list %}
{{user}}
Trouble is because {{user}} is returned as a string - I need to convert into a Django User Object, if it exists in the Django DB, or set a varible unRegistered if not so I can do things like:
{ user.get_profile.about }} # get profile information
So I thought I would write my first Django Template Tag so I could use it like this:
{% webapp_user_lookup user %} # my custom tag
{% ifnot unRegistered %}
{{ user.get_profile.about }} # get profile information - would fail with a string
{% endifnot %}
{% endfor %}
The code I use elsewhere to look up a user in a view is:
try:
user = User.objects.get(username__iexact=user)
unRegistered = False
if not other_user.is_active:
unRegistered = True
except:
unRegistered = True
However looking at the Django Template tag examples I'm having trouble understanding how best to structure the custom template tag, to take my string username - and send back the results as an object if they exist, or set a varible if not and the original string. I'd really like to better understand how the structure works, and if I need a 'class' and if so, why. (I'm new to programming).
use a template filter like so:
{{username|get_user}}
in your user_template_tags.py:
from django import template
from django.contrib.auth.models import User
register = template.Library()
########################
def get_user(username):
try:
user = User.objects.get(username__iexact=username)
except User.DoesNotExist:
user = User.objects.none()
return user
register.filter('get_user',get_user)
then in your template you can do something like:
{% with username|getuser as user %}
{% if user %}DO USER STUFF
{% else %}DO UNREGISTERED STUFF
{% endif %}
{% endwith %}

Categories