Problem statement
The gallery is a heading and nothing else. There is no stylesheet to find, because no asset lives in the app yet and the page asks for one at a typed path that serves nothing. There is no upload, because the form has no image field and the page's form tag cannot carry a file. There is no listing, because Photo has nowhere to keep an image. Build the whole asset story on one page: what ships with the code, what arrives from outside, and how each one gets its URL.
Requirements
Implement:
- the image field on Photo in app/models.py
- the image field and its rules on PhotoForm in app/forms.py
- the page in app/templates/app/gallery.html
- move assets/gallery.css and assets/photo-placeholder.svg into the app's own static directory, app/static/app/
The following files are already implemented and provided: app/views.py and app/urls.py. Photo already holds title and uploaded_at, and PhotoForm already declares title. GET /gallery/ renders app/templates/app/gallery.html with form and photos (oldest first). POST /gallery/ hands the form request.POST and request.FILES, creates a Photo from cleaned_data and redirects on success, or re-renders the same page with status 400 on failure. The finished stylesheet and placeholder image are provided in the top-level assets/ folder (assets/gallery.css and assets/photo-placeholder.svg), but nothing serves that folder. MEDIA_URL is /media/, MEDIA_ROOT is a media directory beside the code, STATIC_URL is /static/, and under manage.py test the tables are built straight from the models, so a model change needs no migration to be testable.
1. The app's own static assets
- Move assets/gallery.css to app/static/app/gallery.css (create the static/app/ folders inside app/). It is then found by the finders as app/gallery.css and served at /static/app/gallery.css. Keep its contents; you do not need to write any CSS.
- Move assets/photo-placeholder.svg to app/static/app/photo-placeholder.svg the same way. It is the image shown for a photo with no image, found as app/photo-placeholder.svg and served at /static/app/photo-placeholder.svg.
- An app nests its own files under a folder named after itself, because everything inside the app static/ directories is collected into one shared namespace.
2. The image field on Photo
- Photo carries image as an image field storing under gallery/%Y/%m/ inside MEDIA_ROOT, so an upload today lands under gallery/<year>/<month>/ rather than piling a decade into one directory.
- A photo is allowed to leave it empty, so a photo created without an image reads as empty.
3. The image field on PhotoForm, and its two rules
- PhotoForm declares image as a required image field, so cleaned_data["image"] carries the upload and the content itself is checked: a file named .png whose bytes are not an image is refused, which an extension allow-list cannot catch.
- A file whose name does not end in .png, .jpg or .jpeg is invalid, with this message:
Only PNG and JPEG images are accepted.
- A file larger than 1 MB - that is 1024 * 1024 bytes - is invalid, with this message:
The image must be 1 MB or smaller.
- An image of exactly 1 MB is accepted. Both errors land under the image key of form.errors.
- A valid POST /gallery/ answers 302 and stores the title and the file, readable back byte for byte, under gallery/. A refused one answers 400 and stores nothing.
4. The page
- The template loads the static library and links the stylesheet as href="/static/app/gallery.css".
- The upload form is rendered inside a <form method="post"> carrying enctype="multipart/form-data", with a submit button. Without that attribute a browser sends no file content at all.
- The photos render in the order the context gives them, each as one <img class="photo"> alongside its title. A <ul id="photos"> of <li> items with the title in a <span class="title"> is the shape this page is written for, but only the images, their order and the titles being present matter. Each image is found by its class="photo" and its src is read, in render order.
- A photo with an image shows that file's own URL, which starts with /media/. A photo without one shows /static/app/photo-placeholder.svg, resolved through the static tag. Reaching for .url on an empty field raises, so the branch is what stands between an empty record and a 500.
- Replacing a photo's image changes what the page shows, and no literal /assets/, "/static/, '/static/, "/media/ or '/media/ string remains anywhere in the template.
Notes
- Only app/models.py, app/forms.py and app/templates/app/gallery.html change, plus moving the two files from assets/ into app/static/app/.
- Every URL comes from {% static %} or from the field; none is typed. Static files ship with the code, media arrives from outside, and nothing a user sends is written into a static directory.
- Do not change MEDIA_ROOT, MEDIA_URL, STATIC_URL or the given view.
- Judge the extension by the file's name; do not add resizing, thumbnails or a storage backend.
