Learn

As we round things up, it’s a good idea to make sure the user experience is thoughtful by implementing a way to notify the user when an RSVP succeeds or if they need to try again in case an error occurs.

Flask provides us with the flash() method to communicate messages powered by the backend. With flash, Flask can record a message at the end of a request and access it on the next request only. We can thus use flash to notify users whether their important actions succeed or fail.

Consider the second half of the RSVP route from the previous exercise. We can update our code to better notify users of what occurs as follows:

# second half of rsvp route if form.validate_on_submit(): dinner_party = DinnerParty.query.filter_by(id=int(form.party_id.data)).first() # new try block try: dinner_party.attendees += f", {username}" db.session.commit() # find the host of dinner_party host = User.query.filter_by(id=int(dinner_party.party_host_id)).first() flash(f"You successfully RSVP'd to {host.username}'s dinner party on {dinner_party.date}!") # new except block except: flash("Please enter a valid Party ID to RSVP!") return render_template('rsvp.html', user=user, dinner_parties=dinner_parties, form=form)
  • the update to dinner_party.attendees and the commit now occur inside a try block
  • the User model is queried for the user hosting dinner_party and stored in host
  • inside the try block, flash() is given a string message as an argument to notify the user that an RSVP successfully occurred
  • an except block is called when there is an error RSVP’ing
  • inside the except block, flash() is given a string message as an argument to notify the user that they were not able to RSVP successfully

With the route updated, we can access our flashed messages in a template file and display them on our page as follows:

{% with messages = get_flashed_messages() %} {% if messages %} {% for message in messages %} {{ message }} {% endfor %} {% endif %} {% endwith %}
  • the get_flashed_messages() function returns all flashed messages in the last session and saves the messages to messages
  • if there are any messages, we for loop through each message in messages and display the message {{ message }}

It’s best practice to look at your code and evaluate areas where things can go wrong. When you identify these points, you can utilize flash() to provide feedback to the user on what exactly happened and how they can proceed.

Instructions

1.

The flash function has been imported at the top of routes.py. Additionally, we have added try and except blocks to the rsvp() route at the bottom of routes.py to prepare for the use of flash() messages.

Add a flash() message inside the try block of the rsvp() route with the following f-string as an argument:

f"You successfully RSVP'd to {host.username}'s dinner party on {dinner_party.date}!"
2.

Staying inside routes.py, replace the pass inside the except block of the rsvp() route with a flash() message containing the following string:

"Please enter a valid Party ID to RSVP!"
3.

Navigate over to rsvp.html. We have added some code at the top of the file to display all flashed messages near the top of the page.

Replace the empty list with the Flask function that returns all flashed messages from the last session.

Sign up to start coding

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?