0
0
Djangoframework~15 mins

STATIC_URL and STATICFILES_DIRS in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Configure STATIC_URL and STATICFILES_DIRS in Django
📖 Scenario: You are building a Django website that needs to serve static files like images, CSS, and JavaScript. To do this, you must tell Django where to find these static files and how to refer to them in your templates.
🎯 Goal: Set up Django settings to define the URL path for static files and specify the folder where your static files are stored.
📋 What You'll Learn
Create a variable STATIC_URL with the value '/static/' in settings.py.
Create a list variable STATICFILES_DIRS that includes the path BASE_DIR / 'static'.
Use the BASE_DIR variable which is already defined as the project base directory.
Follow Django's recommended syntax for static files configuration.
💡 Why This Matters
🌍 Real World
Web developers use STATIC_URL and STATICFILES_DIRS to manage and serve static assets like stylesheets and images in Django projects.
💼 Career
Knowing how to configure static files is a basic but crucial skill for Django developers working on real websites.
Progress0 / 4 steps
1
Create BASE_DIR variable
In settings.py, create a variable called BASE_DIR that uses Path(__file__).resolve().parent.parent to get the project base directory.
Django
Need a hint?

Use from pathlib import Path at the top, then set BASE_DIR to the parent of the parent folder of the current file.

2
Set STATIC_URL variable
Add a variable called STATIC_URL in settings.py and set it to the string '/static/'.
Django
Need a hint?

STATIC_URL is the URL prefix for static files. Use the exact string '/static/'.

3
Define STATICFILES_DIRS list
Create a list variable called STATICFILES_DIRS that contains one path: BASE_DIR / 'static'.
Django
Need a hint?

STATICFILES_DIRS is a list of folders where Django looks for static files. Use square brackets and include BASE_DIR / 'static'.

4
Complete settings.py static files setup
Ensure the settings.py file includes the import of Path, the BASE_DIR variable, the STATIC_URL string, and the STATICFILES_DIRS list exactly as shown.
Django
Need a hint?

Check that all lines are present and exactly match the required code.