Durante el desarrollo, me gusta implementar archivos estáticos de Django en una configuración lo más cercana posible a la producción. Para lograr esto, envuelvo el backend de Django en una imagen ( Dockerfile ) y los archivos estáticos JS de frontend y Django de backend en otra imagen junto con nginx configurado como servidor web ( Dockerfile_nginx ). La configuración es la siguiente:
Estructura del sistema de archivos:
<projekt-repo> /frontend /backend /static (generated with python manage.py collectstatic) settings.py manage.py nginx.conf Dockerfile Dockerfile_nginx settings.py :
STATIC_ROOT = os.path.join(BASE_DIR, "backend/static") STATIC_URL = '/static/' STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")] nginx.conf :
server { listen 0.0.0.0:8080; root /var/www; location / { try_files $uri $uri/ /index.html; } } server { listen 0.0.0.0:8000; root /var/www/django; location /static/ { autoindex on; alias /var/www/django/static/; } } Dockerfile_nginx :
FROM nginx:1.17.8-alpine COPY nginx.conf /etc/nginx/conf.d/nginx.conf COPY edge_frontend/www /var/www COPY edge_backend/static /var/www/django/static Si ejecuto la aplicación e intento iniciar sesión a través del sitio de administración de Django ( localhost:8000/admin ), el sitio no tiene el estilo adecuado y los estados de salida del registro
backend | Not Found: /static/admin/css/base.css backend | Not Found: /static/admin/css/login.css backend | Not Found: /static/admin/css/responsive.css backend | Not Found: /favicon.ico backend | Not Found: /static/admin/css/base.css backend | Not Found: /static/admin/css/login.css backend | Not Found: /static/admin/css/responsive.css backend | Not Found: /favicon.icoObviamente, hay algún desajuste en la configuración que no puedo detectar en este momento. ¿Alguien puede ayudar?
cree una carpeta en su proyecto con el nombre "estático" y luego
agregue en su configuración.py
STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static')o quieres otro camino?
Debe configurar BASE_DIR , STATIC_URL y STATIC_ROOT en settings.py como se muestra a continuación...
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATIC_URL = '/static/' Y ejecute python manage.py collectstatic