Configure Django's STATIC_URL
In your settings.py:
Python
STATIC_URL = '///en/static/' Replace and with the actual values.
In your settings.py:
Python
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') This is where Django will collect all static files during the collectstatic management command.
Create a new .htaccess file in your :
Apache
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^static/(.*)$ //static/$1 [L] Replace with the absolute path to your project's root directory. This rule will redirect requests for static files to the appropriate location in your filesystem.Run the following command in your terminal:
Bash
python manage.py collectstatic This will collect all static files from your apps' static directories and place them in the STATIC_ROOT directory.
Restart your Apache server to apply the changes. Explanation:
STATIC_URL: This setting tells Django the URL prefix for all your static files. Since you're running Django in a subdirectory, you need to include the subdirectory in the URL. STATIC_ROOT: This setting defines the absolute path where Django will collect and serve static files. Apache Configuration: The .htaccess file redirects requests for static files to the correct location on your server. collectstatic: This command gathers all static files from your apps and places them in the STATIC_ROOT directory, making them accessible to Apache. Important Notes:
Security: For production environments, consider using a dedicated static file server like Nginx or a CDN for better performance and security. Debug Mode: If you're in debug mode, Django will serve static files directly from your apps' static directories. The above configuration is primarily for production environments. Permissions: Ensure that Apache has the necessary permissions to read and serve files from the STATIC_ROOT directory. By following these steps, you should be able to successfully serve static files from your Django application running in a subdirectory under Apache.