Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Flask class.
Flask
from flask import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using lowercase flask instead of Flask
✗ Incorrect
The Flask class is imported from the flask module to create the app.
2fill in blank
mediumComplete the code to get the uploaded file from the request.
Flask
file = request.files.get('[1]')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'filename' which is a property, not the input name
Using 'upload' or 'data' which are not standard input names
✗ Incorrect
The key used to get the uploaded file from request.files should match the form input name, commonly 'file'.
3fill in blank
hardFix the error in saving the uploaded file securely.
Flask
filename = [1](file.filename)
file.save(filename) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Saving file.filename directly without sanitizing
Using str() which does not sanitize
Using os.path.basename which only strips directories
✗ Incorrect
Use secure_filename from werkzeug.utils to avoid unsafe file names.
4fill in blank
hardFill both blanks to check if the file is allowed and save it.
Flask
if file and '[1]' in file.filename and file.filename.rsplit('.', 1)[1].lower() [2] ALLOWED_EXTENSIONS: filename = secure_filename(file.filename) file.save(filename)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'startswith' instead of checking for '.'
Using 'not in' which reverses the logic
✗ Incorrect
Check if filename contains a dot and if the extension is in allowed extensions.
5fill in blank
hardFill all three blanks to create a dictionary of filenames and their sizes for allowed files.
Flask
files_info = {file.filename: [1] for file in files if '.' in file.filename and file.filename.rsplit('.', 1)[1].lower() [2] ALLOWED_EXTENSIONS and file [3] None} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using file.size which is not a standard attribute
Using 'is' instead of 'is not' for None check
✗ Incorrect
We read the file content length, check extension membership, and ensure file is not None.