From 1548d03ef3afd7a02cf62b721b18da5f8db89d3f Mon Sep 17 00:00:00 2001 From: crunk Date: Sat, 16 Nov 2024 10:32:31 +0100 Subject: [PATCH] list of files --- templates/upload.html | 12 +++++++++--- upload.py | 23 +++++++++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/templates/upload.html b/templates/upload.html index 29ecc39..1892a31 100644 --- a/templates/upload.html +++ b/templates/upload.html @@ -8,18 +8,17 @@ body { background-image: url({{ url_for('static', filename='content/glit_background.gif')}}); } - #uploadform{ + .box { background: #f9f871; margin: auto; padding: 1em; width: 30em; min-height: 5em; } - -
+ {{ imageuploadform.csrf_token }}
Upload an image here @@ -31,4 +30,11 @@
{{ imageuploadform.submit }}
+
+
    + {% for uploaded_file in uploaded_files %} +
  1. {{uploaded_file}}
  2. + {% endfor %} +
+
diff --git a/upload.py b/upload.py index 917137f..ba5a7ab 100644 --- a/upload.py +++ b/upload.py @@ -14,11 +14,18 @@ ALLOWED_FILES = ["jpg", "png", "gif", "webp"] # don't config these lines APP.config["SECRET_KEY"] = os.urandom(24) + class ImageUploadForm(FlaskForm): """Image upload form.""" + image = FileField( "image:", - validators=[FileAllowed(ALLOWED_FILES, f"Images only, please use any of the following file extensions: {(", ").join(ALLOWED_FILES)}")], + validators=[ + FileAllowed( + ALLOWED_FILES, + f"Images only, please use any of the following file extensions: {(", ").join(ALLOWED_FILES)}", + ) + ], ) submit = SubmitField("Submit") @@ -32,10 +39,22 @@ def saveimage(image): def index(): """Upload route, a page to upload an image""" imageuploadform = ImageUploadForm() + uploaded_files = sorted( + [ + os.path.join(APP.config["IMAGE_FOLDER"], file) + for file in os.listdir(APP.config["IMAGE_FOLDER"]) + ], + key=os.path.getctime, + ) + print(uploaded_files) if request.method == "POST": if imageuploadform.validate_on_submit(): saveimage(imageuploadform.image.data) - return render_template("upload.html", imageuploadform=imageuploadform) + return render_template( + "upload.html", + imageuploadform=imageuploadform, + uploaded_files=uploaded_files + ) if __name__ == "__main__":