What is the purpose of the static folder in a Flask project?
The static folder holds files like images, CSS, and JavaScript that the browser can load directly. It keeps these files separate from Python code for better organization.
Click to reveal answer
beginner
How do you reference a static file in a Flask HTML template?
Use the url_for('static', filename='path/to/file') function. For example, <img src="{{ url_for('static', filename='images/logo.png') }}"> loads an image from the static folder.
Click to reveal answer
beginner
Why should static files be organized into subfolders like css, js, and images inside the static folder?
Organizing static files into subfolders helps keep the project tidy and makes it easier to find and manage files, just like sorting your documents into labeled folders at home.
Click to reveal answer
beginner
What happens if you place a CSS file outside the static folder in Flask?
Flask will not serve the CSS file automatically, so the browser won’t load it. Static files must be inside the static folder to be accessible via URLs.
Click to reveal answer
intermediate
How can you customize the static folder location in a Flask app?
You can set the static_folder parameter when creating the Flask app, like app = Flask(__name__, static_folder='assets'). This changes where Flask looks for static files.
Click to reveal answer
Where should you put JavaScript files in a Flask project?
AInside the <code>static/js</code> folder
BInside the <code>templates</code> folder
CIn the root project folder
DInside the <code>instance</code> folder
✗ Incorrect
JavaScript files belong in the static/js folder so Flask can serve them as static content.
How do you link a CSS file named style.css located in static/css in a Flask template?
Use url_for('static', filename='css/style.css') to generate the correct URL for static files.
What is the default name of the folder Flask uses to serve static files?
Aassets
Bpublic
Cstatic
Dmedia
✗ Incorrect
By default, Flask serves static files from the static folder.
If you want to change the static folder to assets, how do you do it?
Aapp = Flask(__name__, static_folder='assets')
Bapp.static_folder = 'assets'
CSet STATIC_FOLDER = 'assets' in config
DRename the <code>static</code> folder to <code>assets</code> without code changes
✗ Incorrect
You specify the static folder when creating the Flask app with static_folder='assets'.
Why is it important to keep static files separate from Python code in Flask?
ATo make the app run faster
BBecause Python cannot read static files
CTo prevent static files from running as code
DTo improve code readability and browser access
✗ Incorrect
Separating static files keeps the project organized and allows browsers to load these files directly.
Explain how to organize and reference static files in a Flask project.
Think about how you keep your desk tidy with folders and labels.
You got /3 concepts.
Describe what happens if a static file is placed outside the static folder in Flask.
Imagine trying to find a book that is not on the shelf where you expect it.
You got /3 concepts.
Practice
(1/5)
1. Where should you place CSS, JavaScript, and image files in a Flask project to serve them correctly?
easy
A. Inside the static folder
B. Inside the templates folder
C. In the root project directory
D. Inside the views folder
Solution
Step 1: Understand Flask static file convention
Flask expects static files like CSS, JS, and images to be inside a folder named static for automatic serving.
Step 2: Identify correct folder for static files
Files placed in templates or other folders won't be served as static assets by Flask.
Final Answer:
Inside the static folder -> Option A
Quick Check:
Static files = static folder [OK]
Hint: Static files always go in the 'static' folder [OK]
Common Mistakes:
Putting CSS or JS inside the templates folder
Placing images in the root directory
Not using a static folder at all
2. Which of the following is the correct way to link a CSS file named style.css located inside a css folder within the static directory in a Flask template?
easy
A. <link href="/css/style.css" rel="stylesheet">
B. <link href="{{ url_for('static', filename='css/style.css') }}" rel="stylesheet">
C. <link href="static/css/style.css" rel="stylesheet">
D. <link href="/static/style.css" rel="stylesheet">
Solution
Step 1: Use Flask's url_for to generate static file paths
Flask recommends using url_for('static', filename='...') to get the correct URL for static files, ensuring it works in all environments.
Step 2: Check the filename path inside url_for
The CSS file is inside css folder, so the filename should be css/style.css.
Final Answer:
<link href="{{ url_for('static', filename='css/style.css') }}" rel="stylesheet"> -> Option B
Quick Check:
Use url_for('static', filename=path) for static links [OK]
Hint: Always use url_for('static', filename='path') in templates [OK]
4. You wrote this in your Flask template to include an image: <img src="{{ url_for('static', filename='images/logo.png') }}" alt="Logo"> But the image does not show up in the browser. What is the most likely cause?
medium
A. The url_for function cannot be used for images
B. The alt attribute is missing
C. The filename should not include the folder name
D. The static folder is missing or misplaced in the project
Solution
Step 1: Check static folder location
Flask serves static files only if the static folder is in the correct place (usually project root). If missing or misplaced, files won't be found.
Step 2: Validate usage of url_for and filename
The url_for usage is correct including folder name. The alt attribute does not affect image loading.
Final Answer:
The static folder is missing or misplaced in the project -> Option D
Quick Check:
Static folder must exist and be correctly placed [OK]
Hint: Ensure 'static' folder exists at project root [OK]
Common Mistakes:
Forgetting to create the static folder
Placing static folder inside templates
Removing folder path from filename
5. You want to organize your static files neatly by placing CSS files in a css folder, JavaScript files in a js folder, and images in an images folder inside the static directory. Which of the following Flask template snippets correctly links a CSS file named main.css located in static/css/ and a JavaScript file named app.js located in static/js/?