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?
A'route'
B'static'
C'template'
D'file'
✗ 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?
AInside the <code>static</code> folder
BInside the <code>templates</code> folder
CIn the root project folder
DInside the <code>views</code> folder
✗ Incorrect
Static files belong in the static folder so Flask can serve them properly.
What does url_for('static', filename='js/app.js') return?
AAn error because of the slash in filename
BThe content of the <code>app.js</code> file
CThe URL path to the <code>app.js</code> file inside the <code>static/js</code> folder
DThe URL to a Flask route named 'js/app.js'
✗ 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?
ABecause static files cannot be accessed without <code>url_for</code>
BBecause hardcoded URLs are slower to load
CBecause Flask does not allow hardcoded URLs
DBecause <code>url_for</code> handles URL changes and deployment differences automatically
✗ 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?
A404 Not Found
B301 Moved Permanently
C200 OK
D500 Internal Server Error
✗ 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.
Practice
(1/5)
1. What is the main purpose of using url_for('static', filename='...') in a Flask app?
easy
A. To define a new route for dynamic pages
B. To create a new static file in the project folder
C. To generate the correct URL for static files like CSS or images
D. To run the Flask development server
Solution
Step 1: Understand the role of url_for in Flask
url_for is used to build URLs for routes or static files dynamically.
Step 2: Recognize the use of 'static' endpoint
Using url_for('static', filename='...') specifically generates the URL for static files like CSS, JavaScript, or images.
Final Answer:
To generate the correct URL for static files like CSS or images -> Option C