Recall & Review
beginner
What is the purpose of
url_for('static', filename='...') in Flask?It generates the correct URL to access static files like CSS, JavaScript, or images stored in the
static folder of a Flask app.Click to reveal answer
beginner
Where should static files be placed in a Flask project to be served correctly?
Static files should be placed inside the
static folder at the root of the Flask project.Click to reveal answer
beginner
How do you use
url_for to link a CSS file named style.css located in the static folder?Use
url_for('static', filename='style.css') inside your HTML template to get the URL for style.css.Click to reveal answer
intermediate
Why is it better to use
url_for('static', filename='...') instead of hardcoding static file paths?Because
url_for automatically handles URL building, including any changes in app structure or deployment settings, making your links reliable and portable.Click to reveal answer
intermediate
What happens if you try to access a static file that does not exist using
url_for('static', filename='missing.png')?The URL will still be generated, but when accessed in the browser, it will return a 404 error because the file is missing.
Click to reveal answer
What argument do you pass to
url_for to get a static file URL?✗ Incorrect
You use
url_for('static', filename='...') to get URLs for static files.Where should you place your CSS and image files in a Flask project?
✗ Incorrect
Static files belong in the
static folder so Flask can serve them properly.What does
url_for('static', filename='js/app.js') return?✗ Incorrect
It returns the URL to the static file
app.js inside the js folder.Why should you avoid hardcoding static file URLs in Flask templates?
✗ Incorrect
url_for makes your app flexible by adapting URLs if the app structure or deployment changes.What HTTP status code will you get if a static file URL generated by
url_for points to a missing file?✗ Incorrect
If the file is missing, the server returns a 404 Not Found error when the URL is accessed.
Explain how to use
url_for to link a static image in a Flask template.Think about how Flask knows where static files live and how to build their URLs.
You got /4 concepts.
Why is using
url_for('static', filename='...') considered a best practice over hardcoding static file paths?Consider what happens if you move your app or change folder names.
You got /4 concepts.