0
0
Flaskframework~20 mins

URL_for with static files in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Static File Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What URL does url_for('static', filename='style.css') generate?

In a Flask app, you use url_for('static', filename='style.css') inside a template. What URL will this produce by default?

A/style.css
B/static/style.css
C/staticfiles/style.css
D/assets/style.css
Attempts:
2 left
💡 Hint

Think about the default folder Flask uses for static files.

📝 Syntax
intermediate
1:30remaining
Which url_for call correctly references a static image?

Choose the correct Flask url_for syntax to get the URL for logo.png inside the static/images folder.

Aurl_for('static', filename='/images/logo.png')
Burl_for('static/images', filename='logo.png')
Curl_for('static', filename='images/logo.png')
Durl_for('static', 'images/logo.png')
Attempts:
2 left
💡 Hint

Remember the filename argument is a relative path inside the static folder.

🔧 Debug
advanced
2:00remaining
Why does url_for('static', filename='css/style.css') return 404?

You have a file at project/static/css/style.css. Your template uses url_for('static', filename='css/style.css') but the browser shows a 404 error. What is the most likely cause?

AThe static folder is renamed or moved, so Flask can't find the file.
BThe filename argument should not include subfolders like 'css/'.
CYou must use <code>url_for('static', filename='/css/style.css')</code> with a leading slash.
DFlask does not serve static files by default; you must add a route.
Attempts:
2 left
💡 Hint

Check if the static folder location matches Flask's configuration.

state_output
advanced
1:30remaining
What is the output of this Flask template snippet?

Given this Flask template code:

<link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">

And the app is running with default static folder, what will the rendered HTML look like?

A<link rel="stylesheet" href="/assets/css/main.css">
B<link rel="stylesheet" href="/css/main.css">
C<link rel="stylesheet" href="static/css/main.css">
D<link rel="stylesheet" href="/static/css/main.css">
Attempts:
2 left
💡 Hint

Remember how url_for builds URLs for static files.

🧠 Conceptual
expert
2:30remaining
How to serve static files from a custom folder using url_for?

You want Flask to serve static files from a folder named assets instead of the default static. Which setup and url_for usage is correct?

ASet <code>app = Flask(__name__, static_folder='assets')</code> and use <code>url_for('static', filename='file.js')</code>
BKeep default app and use <code>url_for('assets', filename='file.js')</code>
CSet <code>app = Flask(__name__, static_url_path='/assets')</code> and use <code>url_for('static', filename='file.js')</code>
DRename the folder to <code>static</code> and use <code>url_for('assets', filename='file.js')</code>
Attempts:
2 left
💡 Hint

Check Flask's static_folder parameter and how url_for references static files.