0
0
Djangoframework~10 mins

Static files in development in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Static files in development
Start Django development server
Request for static file
Django checks STATIC_URL and STATICFILES_DIRS
Serve static file from local directory
Browser receives and displays static file
Static file visible in browser
When running Django's development server, static file requests are intercepted and served from local folders defined in settings, so you see CSS, images, and JS load correctly.
Execution Sample
Django
1. In settings.py:
   STATIC_URL = '/static/'
   STATICFILES_DIRS = [BASE_DIR / 'static']
2. Run: python manage.py runserver
3. Browser requests /static/style.css
4. Django serves style.css from static folder
This setup lets Django serve static files like CSS during development without extra setup.
Execution Table
StepActionInput/RequestDjango BehaviorOutput/Result
1Start serverpython manage.py runserverServer runs with static settingsReady to serve requests
2Browser requests static fileGET /static/style.cssChecks STATIC_URL and STATICFILES_DIRSLocates style.css in static folder
3Serve static filestyle.css foundReads file contentReturns CSS content to browser
4Browser renders pageReceives CSS contentApplies stylesPage styled with CSS
5Request non-static URLGET /homeNormal view processingRenders HTML page
6Request missing static fileGET /static/missing.jsFile not foundReturns 404 error
💡 Static files served only if found in STATICFILES_DIRS; otherwise 404 returned.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
STATIC_URLNone'/static/''/static/''/static/''/static/'
STATICFILES_DIRSNone[BASE_DIR / 'static'][BASE_DIR / 'static'][BASE_DIR / 'static'][BASE_DIR / 'static']
Requested FileNone'/static/style.css''style.css' found'style.css' servedN/A
ResponseNonePendingCSS contentCSS appliedN/A
Key Moments - 3 Insights
Why does Django serve static files automatically only in development?
Because the development server has built-in static file handling using STATIC_URL and STATICFILES_DIRS, as shown in steps 2 and 3 of the execution_table. In production, a separate web server handles static files.
What happens if the requested static file is missing?
As shown in step 6 of the execution_table, Django returns a 404 error because it cannot find the file in STATICFILES_DIRS.
Why do we set STATIC_URL and STATICFILES_DIRS in settings.py?
These settings tell Django where to look for static files and what URL prefix to use, enabling it to serve files correctly during development (see variable_tracker and steps 2-3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does Django do at step 3 when the static file is found?
AReturns a 404 error
BReads the file content and returns it to the browser
CRedirects to another URL
DIgnores the request
💡 Hint
Check the 'Django Behavior' and 'Output/Result' columns in step 3 of the execution_table.
According to variable_tracker, what is the value of STATIC_URL after starting the server?
ANone
B'/media/'
C'/static/'
DEmpty string
💡 Hint
Look at the STATIC_URL row in variable_tracker after Step 2.
If you remove the static folder from STATICFILES_DIRS, what will happen when requesting /static/style.css?
ADjango will return a 404 error
BDjango will serve the file from another folder automatically
CThe browser will load a cached version
DThe server will crash
💡 Hint
Refer to the exit_note and step 6 in the execution_table about missing static files.
Concept Snapshot
Static files in Django development:
- Set STATIC_URL (e.g., '/static/') in settings.py
- Define STATICFILES_DIRS with local static folders
- Run 'python manage.py runserver' to auto-serve static files
- Requests to STATIC_URL serve files from STATICFILES_DIRS
- Missing files return 404 errors
- This auto-serving works only in development mode
Full Transcript
When you run Django's development server, it automatically serves static files like CSS, JavaScript, and images from folders you specify in STATICFILES_DIRS. The STATIC_URL setting defines the URL prefix for these files. When the browser requests a static file URL, Django looks in the static folders and returns the file content if found. If the file is missing, Django returns a 404 error. This behavior helps you see your styles and scripts during development without extra setup. However, in production, static files are served by a web server, not Django. The execution table shows each step from starting the server, requesting a static file, Django serving it, and the browser rendering it. The variable tracker shows how settings and request variables change during this process. Key moments clarify why static files are served only in development and what happens if files are missing. The quiz tests understanding of these steps and settings.