Skillversitas

Static and Media Files

The static is the file the no the upload: the static (the CSS, the JS, the image, the repo the 1 the place), the media (the upload, the user, the disk the no the repo), and the collectstatic (the 1 the dir, the nginx the serve, the Django the no). The {% static %} is the URL (the STATIC_URL the prefix, the no the hardcode), the STATICFILES_DIRS is the 2 the place (the app, the global), and the storage is the S3 (the 12factor, the no the disk). This chapter covers the static (the 2 the place, the {% static %}), the collectstatic (the 1 the dir, the no the Django), the media (the upload, the storage), and the S3 (the 12factor, the no the disk).

The static: The 2 place and the collectstatic

# the STATIC (the 2, the place):
#   the 1: the app (the app's file):
#   notes_app/static/notes_app/
#     the css/notes.css
#     the js/notes.js
#   the no: the 1 name (the no
#     the clash, the no the 2)
#   the 2: the global (the no
#     the app, the 1 the place):
#   notes/static/css/site.css
#   the settings (the 3):
DEBUG = True   #   the serve (the
                #     the no the
#               #     the collect)
#   the STATIC_URL = "static/"
#   the STATIC_ROOT = BASE_DIR /
#     "staticfiles"   #   the
#                     #     the collect
#   the STATICFILES_DIRS = [
#       BASE_DIR / "static",
#   ]
#   the staticfiles the INSTALLED_APPS
#     (the no, the 12 the no)

# the COLLECTSTATIC (the 1 dir, the
#   no Django, the CI's):
python manage.py collectstatic \
    --noinput
#   the the no: the static/ (the
#     the app, the the global) the
#     the staticfiles/ (the 1, the
#     the no the app's, the nginx's)
#   the 2: the manifest (the hash,
#     the cache, the 1 year):
#   the STORAGES = {
#     "default": {...},
#     "staticfiles": {
#         "BACKEND":
#         "django.contrib.staticfiles."
#         "storage.ManifestStaticFiles"
#         "Storage",
#     },
# }
#   the site.css the site.abc123.css
#     (the hash, the no
#       the stale)

The static is the 2 the place: the app (the notes_app/static/notes_app/, the 1 name, the no the clash) and the global (the notes/static/, the STATICFILES_DIRS, the 1 the place the no the app) - the {% static 'css/site.css' %} is the URL (the STATIC_URL the prefix, the no the hardcode, the the the change the the the no), and the django.contrib.staticfiles is the INSTALLED_APPS (the no, the 12 the no the the). The collectstatic is the 1 the dir: the --noinput is the CI (the no the prompt), the STATIC_ROOT is the 1 (the staticfiles/, the no the app's, the nginx the serve), and the ManifestStaticFilesStorage is the hash (the site.css the site.abc123.css, the cache the 1 the year, the stale the no) - the 12factor is the nginx (the Django the no the static the production, the file the 1 the no the gunicorn) - the media is the next: the upload the user the storage.

The media and the S3 storage

# the MEDIA (the upload, the user):
#   the settings (the 2):
#   the MEDIA_URL = "media/"
#   the MEDIA_ROOT = BASE_DIR /
#     "media"
#   the serve (the DEBUG only, the
#     the no the production):
#   the urls (the project):
#   the 1: the 2 import
# from django.conf import settings
# from django.conf.urls.static import \
#     static
# urlpatterns += static(
#     settings.MEDIA_URL,
#     document_root=settings.
#     MEDIA_ROOT)
#   the 2: the no (the production's
#     the no, the nginx or
#     the S3 the yes):
#   the model (the upload):
class Avatar(models.Model):
    image = models.ImageField(
        upload_to="avatars/%Y/%m/")
#   the media/avatars/2024/01/a.jpg
#     (the 2, the 1, the no
#       the 1 dir)
#   the the no: the no the
#     the 1 (the 10K the no,
#     the 1 the no)
# the S3 (the 12factor, the no disk):
#   the 1: the 1 install
#     (the boto3, the no
#       the 1):
#   pip install django-storages[boto3]
#   the 2: the settings (the
#     the 12, the env):
#   the STORAGES = {
#     "default": {
#         "BACKEND":
#         "storages.backends.s3boto3."
#         "S3Boto3Storage",
#         "OPTIONS": {
#             "Bucket": os.environ
#                 .get("AWS_BUCKET"),
#         },
#     },
#     "staticfiles": {
#         "BACKEND":
#         "storages.backends.s3boto3."
#         "S3StaticStorage",
#     },
# }
#   the no: the disk (the scale,
#     the no the 1, the no
#     the no), the S3 the 11
#     the 9 (the durable, the no
#     the 1)

The media is the upload: the MEDIA_URL plus the MEDIA_ROOT is the 2 (the media/, the disk the no the repo), the static() is the DEBUG only (the production the no, the nginx or the S3), and the upload_to is the 2 (the %Y/%m, the 1 the dir the 10K the no) - the 12factor is the S3: the django-storages[boto3] is the 1 the install, the S3Boto3Storage is the default (the upload the S3, the disk the no), the S3StaticStorage is the static (the collectstatic the no, the S3 the 1 the place), and the env is the secret (the key, the 12factor the no the commit) - the rule is the one: the static the 2 the place, the collectstatic the 1 the dir, the media the upload, and the S3 the 12factor - the next chapter's auth is the user the login the session the no the 1 the code.

The Working Rules

  • The static is the 2, the app plus the global. The app the 1 name (the no the clash, the app/model_list.html the no), and the global the STATICFILES_DIRS (the 1 the place) - the {% static %} the URL (the STATIC_URL, the no the hardcode), and the staticfiles the INSTALLED_APPS (the no, the no the tag).
  • The collectstatic is the 1 dir, the Manifest the hash. The --noinput the CI (the no the prompt), the STATIC_ROOT the staticfiles (the no the app's, the nginx's serve), and the ManifestStaticFilesStorage the hash (the .abc123, the cache the 1 year, the stale the no) - the DEBUG the serve (the no the production, the no the gunicorn).
  • The media is the upload, the static() the DEBUG. The MEDIA_URL plus the MEDIA_ROOT (the disk the no the repo), and the static() the DEBUG only (the production the no, the nginx or the S3) - the upload_to the %Y/%m (the 1 the dir, the 10K the no), and the ImageField the Pillow (the no the thumbnail, the 1 the no).
  • The S3 is the 12factor, the disk the no. The django-storages[boto3] the 1, the S3Boto3Storage the upload (the disk the no, the scale the 1), and the S3StaticStorage the static (the collectstatic the no) - the env the secret (the key, the no the commit), and the no the local (the test, the 1 the no).

Summary

The static is the file the no the upload: the static the 2 (the app the 1 name, the global the STATICFILES_DIRS, the {% static %} the URL the no the hardcode, the staticfiles the INSTALLED_APPS), the collectstatic the 1 dir (the --noinput the CI, the STATIC_ROOT the staticfiles, the ManifestStaticFilesStorage the hash the 1 year, the no the stale), and the nginx is the serve (the Django the no the production, the file the 1 the no the gunicorn). The media is the upload (the MEDIA_URL the MEDIA_ROOT, the static() the DEBUG only, the upload_to the %Y/%m, the ImageField the Pillow), and the S3 is the 12factor (the django-storages[boto3], the S3Boto3Storage the upload, the S3StaticStorage the static, the env the secret, the no the disk, the scale the 1). The rule is the one: the static the no the upload, the media the no the repo, the collectstatic the 1 dir, and the S3 the no the disk - the next chapter's auth is the user: the login the no the 1 the code, the session the signed, and the permission the no the 403.