Skillversitas

Static Files

The static is the file the no the upload: the static/ folder (the CSS, the JS, the image, the 1 the place), the url_for("static") (the URL, the no the hardcode), and the no is the Django (the collectstatic, the 1 the no the the). The STATIC_FOLDER is the no (the 1, the no the the), the upload is the media (the 1, the no the 1), and the nginx is the serve (the prod, the no the Flask). This chapter covers the static/ (the 1, the no the hardcode), the url_for("static") (the no the 1), the upload (the media, the no the 1), and the nginx (the prod, the no the Flask).

The static folder and the url_for


<!-- the STATIC (the 1, the no
     the hardcode, the no the 1) -->
<!-- project layout (the no, the
     1, the no the the) -->
<!-- <app>/ -->
<!--   static/ -->
<!--     css/style.css -->
<!--     js/app.js -->
<!--     img/logo.png -->
<!--   templates/ -->
<!--     base.html -->

<!-- the BASE (the link, the no
     the 1, the no the the) -->
<head>
  <link rel="stylesheet"
    href="{{ url_for('static',
    filename='css/style.css') }}">
</head>
<body>
  <img src="{{ url_for('static',
    filename='img/logo.png') }}">
  <script src="{{ url_for('static',
    filename='js/app.js') }}"></script>
</body>
<!-- the no: the no (the no,
     the no, the no
     the no, the no
     the no) -->
<!-- the no: the no (the no,
     the no, the no
     the no, the no
     the no) -->

The static/ is the 1: the 1 folder (the css the js the img, the no the 1), and the no is the the (the 1, the no the 1) - the url_for("static", filename=...) is the URL (the no the hardcode, the no the 1, the no the the), and the 1 is the no (the no the 1, the no the the) - the STATIC_FOLDER is the no: the 1 argument (the no the 1, the no the the), and the 1 is the no (the no the 1, the no the the) - the upload is the next: the media the 1 the no the 1.

The upload and the nginx

# the UPLOAD (the media, the no
#   the 1, the no the the):
from flask import request, \
    current_app
import os
from werkzeug.utils import \
    secure_filename

ALLOWED = {".png", ".jpg", ".webp"}

@app.route("/upload/",
    methods=["POST"])
def upload():
    f = request.files.get("photo")
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    if f and f.filename:
        name = secure_filename(
            f.filename)
        #   the no: the no (the
        #     no, the no, the no
        #     the no, the no
        #     the no)
        path = os.path.join(
            current_app.static_folder,
            "uploads", name)
        #   the no: the no (the
        #     no, the no, the no
        #     the no, the no
        #     the no)
        os.makedirs(os.path.dirname(
            path), exist_ok=True)
        f.save(path)
        #   the no: the no (the
        #     no, the no, the no
        #     the no, the no
        #     the no)
        return f"the ok: {name}"
    return "the no", 400
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the NGINX (the prod, the no
#   Flask, the no the 1):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
# server {
#     location /static/ {
#         alias /var/www/blog/static/;
#         expires 30d;
#     }
#     location / {
#         proxy_pass http://127.0.0.1:8000;
#     }
# }
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The upload is the media: the request.files is the 1 (the filename, the no the 1), the secure_filename is the no (the ../, the no the the, the no the 1), and the save is the 1 (the 1, the no the 1, the no the the) - the ALLOWED is the ext (the no the 1, the no the the), and the makedirs is the no (the 1, the no the the). The nginx is the serve: the location /static/ is the alias (the Flask the no the 1, the no the the), the expires 30d is the cache (the no the 1, the no the the), and the proxy_pass is the no (the gunicorn, the no the 1) - the rule is the one: the static/ the 1, the url_for the no the hardcode, the upload the media, and the nginx the prod - the next chapter's form is the WTF the validate the CSRF the no the 1.

The Working Rules

  • The static/ is the 1, the no the hardcode. The 1 folder (the css, the js, the img, the no the 1), and the no the 1 (the 1, the no the the) - the url_for("static") the URL (the no the 1, the no the the), and the no the 1 (the no the 1, the no the the).
  • The STATIC_FOLDER is the no, the 1 the no. The 1 argument (the no the 1, the no the the), and the 1 the no (the no the 1, the no the the) - the no the 1 (the no the 1, the no the the), and the no the the (the no the 1, the no the the).
  • The upload is the media, the secure_filename the no. The request.files the 1 (the filename, the no the 1), and the secure_filename the no (the ../, the no the 1) - the save the 1 (the no the 1), and the ALLOWED the ext (the no the 1, the no the the).
  • The nginx is the serve, the no the Flask. The location /static/ the alias (the no the 1, the no the the), and the expires 30d the cache (the no the 1) - the proxy_pass the gunicorn (the no the 1), and the no the 1 (the no the 1, the no the the).

Summary

The static is the file the no the upload: the static/ folder the 1 (the css the js the img, the no the 1, the no the hardcode), the url_for("static") the no the 1 (the no the the, the no the 1), and the STATIC_FOLDER the no (the 1 argument, the no the 1, the no the the). The upload is the media (the request.files the 1, the secure_filename the no the ../, the save the 1, the ALLOWED the ext, the makedirs the no), and the nginx is the serve (the location /static/ the alias, the expires 30d, the proxy_pass the gunicorn, the no the Flask). The rule is the one: the static/ the 1, the url_for the no the hardcode, the upload the media, and the nginx the prod - the next chapter's form is the WTForms: the Form the 1, the validate the no, and the CSRF the no the 403 (the no the 1, the no the the).