0
0
Djangoframework~20 mins

File upload handling basics in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Upload Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a file is uploaded using Django's default FileField?

Consider a Django model with a FileField. When a user uploads a file through a form linked to this model, what is the default behavior regarding the file storage?

Django
class Document(models.Model):
    upload = models.FileField(upload_to='uploads/')
AThe file is uploaded but not saved anywhere unless manually handled in the view.
BThe file is stored directly in the database as binary data.
CThe file is saved in a temporary folder and deleted after the request finishes.
DThe file is saved to the directory specified by <code>upload_to</code> inside the <code>MEDIA_ROOT</code> folder.
Attempts:
2 left
💡 Hint

Think about where Django stores uploaded files by default when using FileField.

📝 Syntax
intermediate
2:00remaining
Identify the correct way to handle file upload in a Django view

Which of the following Django view snippets correctly handles a file upload from a form?

Django
def upload_file(request):
    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            # What should happen here?
            pass
    else:
        form = UploadForm()
    return render(request, 'upload.html', {'form': form})
Afile = request.FILES['file']; instance = Document(); instance.upload = file; instance.save()
Bfile = request.POST['file']; instance = Document(upload=file); instance.save()
Cfile = request.FILES.get('file'); instance = Document(); instance.upload = file; instance.save()
Dfile = request.FILES['file']; instance = Document(upload=file); instance.save()
Attempts:
2 left
💡 Hint

Remember that uploaded files are in request.FILES and you can assign them to model fields before saving.

🔧 Debug
advanced
2:00remaining
Why does this Django file upload view raise a MultiValueDictKeyError?

Examine the following Django view code snippet. It raises a MultiValueDictKeyError when no file is uploaded. Why?

Django
def upload_file(request):
    if request.method == 'POST':
        file = request.FILES['file']
        instance = Document(upload=file)
        instance.save()
    return render(request, 'upload.html')
ABecause <code>request.FILES['file']</code> assumes the file key always exists, but it may be missing if no file is uploaded.
BBecause the <code>Document</code> model does not have a <code>FileField</code> named <code>upload</code>.
CBecause the view does not check if the request method is GET before accessing <code>request.FILES</code>.
DBecause the file is too large and Django rejects it silently.
Attempts:
2 left
💡 Hint

Think about what happens if the form is submitted without a file.

state_output
advanced
2:00remaining
What is the value of the model field after saving a file upload?

Given this Django model and view, what will be the value of instance.upload.name after saving a file named example.txt?

Django
class Document(models.Model):
    upload = models.FileField(upload_to='docs/')

def upload_file(request):
    if request.method == 'POST':
        file = request.FILES['file']
        instance = Document(upload=file)
        instance.save()
        return instance.upload.name
Aexample.txt
Bdocs/example.txt
CMEDIA_ROOT/docs/example.txt
D/docs/example.txt
Attempts:
2 left
💡 Hint

Remember that upload_to sets a folder path relative to MEDIA_ROOT.

🧠 Conceptual
expert
2:00remaining
Why is it important to use request.FILES instead of request.POST for file uploads in Django?

When handling file uploads in Django, why must you access uploaded files via request.FILES instead of request.POST?

A<code>request.FILES</code> is only used for images, while <code>request.POST</code> handles other file types.
B<code>request.POST</code> contains file data but in a compressed format that needs decoding.
C<code>request.FILES</code> contains file data as <code>UploadedFile</code> objects, while <code>request.POST</code> only contains text data from the form fields.
D<code>request.POST</code> is deprecated and should never be used.
Attempts:
2 left
💡 Hint

Think about how browsers send files in forms and how Django separates data.