On Django new version 3.1, the settings file have some changes, and I came to ask how I have to proceed to set my static files? The way that I usually did doesn't work more.
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
from pathlib import Path
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media_root')
If I insert the import os
will work, but is it the right practice?
What is the best practice to set this?
Thank you?
This change makes it a lot easier for you to define your STATIC
and MEDIA
variables. You don't even need to import os
for this purpose and all you need is to add following codes to your settings.py
:
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent # which shows the root directory of your project
STATIC_ROOT = BASE_DIR / 'static' # is equal to os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
MEDIA_ROOT = BASE_DIR / 'media' # is equal to os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = BASE_DIR.parent / "static_cdn"
try this if you want to add your STATIC_ROOT inside environment directory
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With