Challenge - 5 Problems
Static Files Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when accessing a static file URL?
Given this FastAPI app code, what will be the HTTP response status code when accessing
/static/example.txt if the file exists in the static folder?FastAPI
from fastapi import FastAPI from fastapi.staticfiles import StaticFiles app = FastAPI() app.mount("/static", StaticFiles(directory="static"), name="static")
Attempts:
2 left
💡 Hint
Think about what happens when a static file exists and is served by StaticFiles.
✗ Incorrect
When a file exists in the mounted static directory, FastAPI serves it with a 200 OK status and the file content.
📝 Syntax
intermediate2:00remaining
Which option correctly mounts a static directory in FastAPI?
Select the code snippet that correctly mounts the 'assets' folder at the URL path '/assets' using FastAPI.
Attempts:
2 left
💡 Hint
Check the parameter names and URL path format.
✗ Incorrect
The mount method requires the URL path starting with a slash and the directory parameter to specify the folder.
🔧 Debug
advanced2:00remaining
Why does this FastAPI app return 404 for static files?
Consider this code snippet:
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
app = FastAPI()
app.mount("/static", StaticFiles(directory="static_files"), name="static")
The folder 'static_files' exists and contains 'image.png'. Accessing '/static/image.png' returns 404. What is the most likely cause?
Attempts:
2 left
💡 Hint
Check the folder name and path carefully.
✗ Incorrect
If the folder name given to StaticFiles does not match the actual folder name, FastAPI cannot find the files and returns 404.
❓ state_output
advanced2:00remaining
What happens if you mount two static directories on the same path?
Given this FastAPI code:
app.mount("/static", StaticFiles(directory="static1"), name="static1")
app.mount("/static", StaticFiles(directory="static2"), name="static2")
What will be the behavior when accessing '/static/file.txt'?
Attempts:
2 left
💡 Hint
Think about how mounting the same path twice works.
✗ Incorrect
Mounting the same path twice causes the second mount to override the first one, so files come from the second directory.
🧠 Conceptual
expert2:00remaining
Which statement about FastAPI StaticFiles is true?
Choose the correct statement about serving static files with FastAPI's StaticFiles.
Attempts:
2 left
💡 Hint
Think about how web servers handle file types.
✗ Incorrect
StaticFiles uses Starlette's implementation which sets MIME types automatically based on file extensions.