Posted under » Django on 30 April 2021
Define where your static files reside at settings.py.
from pathlib import Path
# BASE_DIR is already defined at the top of your settings.py
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / "static/briles",
BASE_DIR / "static/briles/dashboard",
]
When you create it, it will be empty. Populate the static files folder with
$ python manage.py collectstatic
After running the command, you will see your static folder being populated.
Django itself doesn’t serve static (media) files, such as images, style sheets, or video. It leaves that job to whichever Web server you choose. So when you are using Apache, this is not enough and you will not see the static file being loaded. Unlike PHP, Python separates the static files from codes.
In your apache config file.
Alias /static/ /var/www/dblog/static/
<Directory /var/www/dblog/static/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Reload Apache and you know everything is working well when you load your admin page, it is being formatted nicely.
By default Apache will have directory indexing but for security, you should put a .htaccess file to prevent browsing.