En la nueva versión 3.1 de Django, el archivo de configuración tiene algunos cambios, y vine a preguntar cómo debo proceder para configurar mis archivos estáticos. La forma en que solía hacerlo no funciona más.
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') Si inserto el import os , funcionará, pero ¿es la práctica correcta? ¿Cuál es la mejor práctica para establecer esto? ¿Gracias?
STATIC_ROOT = BASE_DIR.parent / "static_cdn"intente esto si desea agregar su STATIC_ROOT dentro del directorio de entorno
Este cambio le facilita mucho la definición de sus variables STATIC y MEDIA . Ni siquiera necesita importar el os para este propósito y todo lo que necesita es agregar los siguientes códigos a su 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/'