Static files are images, CSS, and JavaScript that make your website look nice and work well. During development, Django helps you serve these files easily without extra setup.
Static files in development in Django
1. In settings.py, add: STATIC_URL = '/static/' 2. Place your static files inside an app folder named 'static', e.g., myapp/static/myapp/style.css 3. In your template, load static and use: {% load static %} <link rel="stylesheet" href="{% static 'myapp/style.css' %}">
The STATIC_URL setting tells Django where to find static files in URLs.
Use the {% load static %} tag in templates to access static files easily.
logo.png from the static/images folder.{% load static %}
<img src="{% static 'images/logo.png' %}" alt="Logo">app.js from the static/js folder.{% load static %}
<script src="{% static 'js/app.js' %}"></script>STATIC_URL = '/static/'This example shows how to set STATIC_URL, organize static files inside an app, and use them in a template. When you run the Django development server, it will serve the CSS file automatically, and the page background will be light blue with a green heading.
# settings.py snippet STATIC_URL = '/static/' # Directory structure: # myproject/ # ├── myapp/ # │ └── static/ # │ └── myapp/ # │ └── style.css # └── templates/ # └── base.html # base.html content: """ {% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="{% static 'myapp/style.css' %}"> <title>Static Files Demo</title> </head> <body> <h1>Welcome to Static Files Demo</h1> </body> </html> """ # style.css content: """ body { background-color: #e0f7fa; font-family: Arial, sans-serif; } h1 { color: #00796b; } """
During development, Django automatically serves static files if DEBUG = True.
In production, you need a web server or service to serve static files separately.
Keep static files organized inside each app's static folder to avoid conflicts.
Static files are needed for styling and scripts in your Django app.
Use STATIC_URL and the {% static %} template tag to include them.
Django serves static files automatically during development for easy testing.
Practice
STATIC_URL setting in a Django project during development?Solution
Step 1: Understand the role of STATIC_URL
STATIC_URL tells Django where to find static files when rendering pages, usually a URL prefix like '/static/'.Step 2: Differentiate from other settings
It is not related to media files, databases, or caching but specifically for static file URLs.Final Answer:
It defines the base URL to access static files like CSS and JavaScript. -> Option AQuick Check:
STATIC_URL = base URL for static files [OK]
- Confusing STATIC_URL with media file settings
- Thinking STATIC_URL controls file storage location
- Assuming STATIC_URL manages caching or databases
styles.css in a Django template?Solution
Step 1: Use the {% static %} template tag
In Django templates, static files should be included using the {% static %} tag to generate the correct URL.Step 2: Check syntax correctness
The correct syntax is <link href="{% static 'filename' %}" rel="stylesheet"> to load static files properly.Final Answer:
<link href="{% static 'styles.css' %}" rel="stylesheet"> -> Option DQuick Check:
Use {% static 'file' %} to link static files [OK]
- Hardcoding /static/ path without {% static %} tag
- Using {{ STATIC_URL }} which is not available by default
- Confusing media URL with static URL
STATIC_URL = '/assets/'
<link href="{% static 'css/main.css' %}" rel="stylesheet">Solution
Step 1: Check STATIC_URL value
STATIC_URL is set to '/assets/', so all static file URLs start with this prefix.Step 2: Apply {% static %} tag behavior
The {% static 'css/main.css' %} tag prepends STATIC_URL to the path, resulting in '/assets/css/main.css'.Final Answer:
/assets/css/main.css -> Option BQuick Check:
STATIC_URL + file path = rendered URL [OK]
- Assuming default '/static/' prefix instead of custom '/assets/'
- Mixing media URL with static URL
- Ignoring STATIC_URL setting in templates
{% static 'images/logo.png' %}, but the image does not show during development. Which is the most likely cause?Solution
Step 1: Check staticfiles app presence
Django serves static files in development only if 'django.contrib.staticfiles' is in INSTALLED_APPS.Step 2: Understand collectstatic role
collectstatic is for production; during development, staticfiles app handles serving automatically.Final Answer:
The django.contrib.staticfiles app is not in INSTALLED_APPS. -> Option CQuick Check:
Missing staticfiles app blocks static serving [OK]
- Running collectstatic unnecessarily in development
- Confusing MEDIA_URL with STATIC_URL
- Ignoring INSTALLED_APPS configuration
assets inside your Django app during development. Which combination of settings and template usage will correctly serve a CSS file assets/css/style.css?Choose the correct settings and template snippet.
Solution
Step 1: Configure STATICFILES_DIRS correctly
STATICFILES_DIRS should be a list of absolute paths; using BASE_DIR / 'assets' ensures correct path to the assets folder.Step 2: Use {% static %} tag with relative path
The template should use {% static 'css/style.css' %} to generate the URL, matching the folder structure inside assets.Final Answer:
In settings.py: STATICFILES_DIRS = [BASE_DIR / 'assets'] and in template: <link href="{% static 'css/style.css' %}" rel="stylesheet"> -> Option AQuick Check:
STATICFILES_DIRS needs absolute path; use {% static %} for URLs [OK]
- Using relative path strings without BASE_DIR
- Confusing STATIC_ROOT with STATICFILES_DIRS
- Setting MEDIA_URL instead of STATICFILES_DIRS
Ready to prove it?
Pass the test and this pattern moves unknown
