0
0
Flaskframework~10 mins

Route naming conventions in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a route for the home page in Flask.

Flask
from flask import Flask
app = Flask(__name__)

@app.route('[1]')
def home():
    return "Welcome!"
Drag options to blanks, or click blank then click option'
A/home
B/index.html
C/
D/main
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/home' instead of '/' for the home page route.
Adding file extensions like '.html' in the route.
2fill in blank
medium

Complete the code to define a route for a user profile page with a dynamic username.

Flask
@app.route('/user/[1]')
def profile(username):
    return f"User: {username}"
Drag options to blanks, or click blank then click option'
A<string:username>
B<username>
C<user>
D<name>
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the type like which works but is less explicit.
Using incorrect variable names like or that don't match the function parameter.
3fill in blank
hard

Fix the error in the route decorator to correctly capture an integer post ID.

Flask
@app.route('/post/[1]')
def show_post(post_id):
    return f"Post {post_id}"
Drag options to blanks, or click blank then click option'
A<post_id>
B<id>
C<integer:post_id>
D<int:post_id>
Attempts:
3 left
💡 Hint
Common Mistakes
Using without type captures a string, not an integer.
Using which is not a valid Flask converter.
4fill in blank
hard

Fill both blanks to create a route for editing a post with an integer ID and a function named edit_post.

Flask
@app.route('/post/[1]/edit')
def [2](post_id):
    return f"Edit post {post_id}"
Drag options to blanks, or click blank then click option'
A<int:post_id>
Bedit_post
Cupdate_post
D<post_id>
Attempts:
3 left
💡 Hint
Common Mistakes
Using without type in the route.
Naming the function update_post instead of edit_post.
5fill in blank
hard

Fill all three blanks to create a route for deleting a comment with integer IDs for post and comment, and a function named delete_comment.

Flask
@app.route('/post/[1]/comment/[2]/delete')
def [3](post_id, comment_id):
    return f"Delete comment {comment_id} from post {post_id}"
Drag options to blanks, or click blank then click option'
A<int:post_id>
B<int:comment_id>
Cdelete_comment
Dremove_comment
Attempts:
3 left
💡 Hint
Common Mistakes
Using string converters instead of int for IDs.
Naming the function remove_comment which is not the expected name.