Learn

Congratulations on building your first Flask app!

You’ve learned to:

  • Import the Flask class and create an application object

    from flask import Flask app = Flask(__name__)
  • Define routes for handling requests sent from various URLs

    @app.route('/') def home(): return '<h1>Hello, World!</h1>'
  • Create variable rules to handle dynamic URLs

    @app.route('/orders/<user_name>/<int:order_id>') def orders(user_name, order_id): return f'<p>Fetching order #{order_id} for {user_name}.</p>'

Time to put what you’ve learned to the test!

Instructions

1.

Define a third view function called article() that is bound to the URL path '/article'. The function should return an <a> tag with the text Return back to home page that links to "/".

Run app.py in the terminal and navigate to http://localhost:5000/article in the browser.

2.

Now, add a variable rule such that a URL whose path follows the pattern '/article/X' will trigger the article() function. Name the variable part article_name.

3.

Update the article() function to take a parameter called article_name. Since this value is the URL slug, assume article_name will be all lower-cased with hyphens separating each word. For example, article_name could look like this:

ten-ducks-enter-local-pond

In the function body, replace the hyphens with spaces and turn the text to title-case. Then, before the <a> element in the returned HTML, add a <h2> heading containing the formatted title. Check the hint for guidance on how to make these formatting changes.

Try visiting various article pages such as http://localhost:5000/article/my-first-flask-app!

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?