0
0
Flaskframework~10 mins

Allowed file types validation in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define allowed file extensions as a set.

Flask
ALLOWED_EXTENSIONS = [1]
Drag options to blanks, or click blank then click option'
A['png', 'jpg', 'jpeg', 'gif']
B('png', 'jpg', 'jpeg', 'gif')
C"png, jpg, jpeg, gif"
D{'png', 'jpg', 'jpeg', 'gif'}
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a set slows down membership checks.
Using a string instead of a collection causes errors.
2fill in blank
medium

Complete the function to check if a filename has an allowed extension.

Flask
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].[1]() in ALLOWED_EXTENSIONS
Drag options to blanks, or click blank then click option'
Alower
Bupper
Cstartswith
Dendswith
Attempts:
3 left
💡 Hint
Common Mistakes
Using startswith or endswith instead of lower.
Not converting to lowercase causes case sensitivity issues.
3fill in blank
hard

Fix the error in the file upload route to validate file type correctly.

Flask
from flask import request, redirect, url_for

@app.route('/upload', methods=['POST'])
def upload_file():
    file = request.files.get('file')
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], [1]))
        return redirect(url_for('uploaded_file', filename=filename))
    return 'Invalid file type', 400
Drag options to blanks, or click blank then click option'
Afilename
Bfile.filename
Cfile
Drequest.filename
Attempts:
3 left
💡 Hint
Common Mistakes
Using file.filename directly without securing it.
Using file or request.filename which are incorrect.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension filtering allowed files and mapping to their extensions.

Flask
files = ['image.png', 'doc.pdf', 'photo.jpg']
allowed_files = {f: f.[1]('.', 1)[1].[2]() for f in files if '.' in f and f.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS}
Drag options to blanks, or click blank then click option'
Arsplit
Bsplit
Cupper
Dlower
Attempts:
3 left
💡 Hint
Common Mistakes
Using split instead of rsplit may cause wrong splits.
Using upper instead of lower causes mismatch with allowed extensions.
5fill in blank
hard

Fill all three blanks to define a Flask route that accepts file uploads and validates allowed types.

Flask
@app.route('/upload', methods=[[1]])
def upload():
    file = request.files.get('file')
    if file and allowed_file(file.[2]):
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], [3]))
        return 'File uploaded successfully'
    return 'Invalid file type', 400
Drag options to blanks, or click blank then click option'
A'POST'
Bfilename
D'GET'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'GET' instead of 'POST' for upload route.
Using wrong attribute instead of file.filename.
Confusing variable names for saving the file.