Problem statement
Applicants attach their CV, hit submit, and the file vanishes. The view is already written correctly - it hands the form both request.POST and request.FILES, because an uploaded file never arrives in POST - but ApplicationForm has no field to receive it, so nothing is looked for and nothing is kept. The page has a matching gap: its form tag is missing the one attribute that makes a browser send a file in the first place.
Requirements
Implement:
- the document field and its rules on ApplicationForm in app/forms.py
- the form tag encoding in app/templates/app/apply.html
The following files are already implemented and provided: app/models.py, app/views.py and app/urls.py. Application holds applicant, document (a FileField(upload_to="applications/")) and submitted_at. project/settings.py sets MEDIA_URL to /media/ and MEDIA_ROOT to a media directory beside the code. apply hands the form both request.POST and request.FILES, because an uploaded file never arrives in POST, and creates the Application from form.cleaned_data["applicant"] and form.cleaned_data["document"]. ApplicationForm already declares applicant.
1. The document field
- ApplicationForm declares document as a required file field, so form.cleaned_data["document"] carries the uploaded file and an application with no document is refused.
- A FileField on a form cleans to an UploadedFile, which knows its name and its size.
2. The two rules, reported on the document field
- A file whose name does not end in .pdf is invalid, with this message:
Only PDF files are accepted.
- A file larger than 2 MB - that is 2 * 1024 * 1024 bytes - is invalid, with this message:
The document must be 2 MB or smaller.
- A document of exactly 2 MB is accepted. One byte more is not.
- Both errors land under the document key of form.errors.
3. The form tag
- The form in apply.html declares enctype="multipart/form-data", and everything else on the page stays as it is. Without it the browser sends no file content at all and the field arrives empty no matter how correct the Python is.
4. What a submission does
- POST /apply/ with a valid application answers 302 and stores one row, with the file written under applications/ inside MEDIA_ROOT and readable back byte for byte.
- POST /apply/ with a refused or missing document answers 400 and stores nothing at all.
Notes
- Only app/forms.py and app/templates/app/apply.html change.
- Do not change MEDIA_ROOT, MEDIA_URL or the model's upload_to. Media arrives from outside the project and never belongs in a static directory; MEDIA_ROOT is where Django writes uploads and upload_to is the subdirectory inside it.
- Judge the extension by the file's name; do not inspect its contents or trust the browser's content type.
- Do not add any package.
