0
0
Flaskframework~10 mins

File upload forms 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 import the Flask class.

Flask
from flask import [1]
Drag options to blanks, or click blank then click option'
Arender_template
BRequest
CFlask
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using lowercase flask instead of Flask
2fill in blank
medium

Complete the code to get the uploaded file from the form.

Flask
file = request.files.get('[1]')
Drag options to blanks, or click blank then click option'
Afile
Bfilename
Cupload
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'filename' which is a property of the file object, not the form field name
Using 'upload' or 'data' which are not the form field names
3fill in blank
hard

Fix the error in saving the uploaded file securely.

Flask
filename = secure_filename(file.[1])
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
Drag options to blanks, or click blank then click option'
Afilename
Bname
Cfile
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Using file.name which does not exist
Using file.file which is incorrect
Using file.path which is not an attribute
4fill in blank
hard

Fill both blanks to check if the request method is POST and a file is present.

Flask
if request.method == '[1]' and '[2]' in request.files:
Drag options to blanks, or click blank then click option'
APOST
Bfile
CGET
Dupload
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'GET' method instead of 'POST'
Checking for wrong file field name in request.files
5fill in blank
hard

Fill all three blanks to create a Flask route that accepts file uploads and saves the file.

Flask
@app.route('/upload', methods=['[1]'])
def upload_file():
    if request.method == '[2]' and '[3]' in request.files:
        file = request.files['file']
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        return 'File uploaded successfully'
    return 'Upload a file'
Drag options to blanks, or click blank then click option'
APOST
BGET
Cfile
DPUT
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET or PUT instead of POST in route methods
Checking for wrong request method
Using wrong file field name