StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 6 · Static & Media Files

Avatar Image Field

easyDjango File UploadsTemplates100%3 of 3 solved

Problem statement

Profiles are meant to show the member's picture, with a neutral silhouette for anyone who has not uploaded one. The model has nowhere to keep a picture, so the page shows no image at all - neither a real one nor a fallback. The placeholder is already in the app's static directory; the field and the branch that chooses between them are not.

Requirements

Implement:

  • the avatar field on Profile in app/models.py
  • the avatar image in app/templates/app/profile.html

The following files are already implemented and provided: app/views.py and app/urls.py. Profile already holds display_name. GET /profiles/<pk>/ renders app/templates/app/profile.html with that profile in the context under profile. MEDIA_URL is /media/ and MEDIA_ROOT is a media directory beside the code. The fallback image already exists, read-only, at app/static/app/avatar-placeholder.svg, and the template already loads the static library.

1. The avatar field

  • Profile carries avatar as an image field storing under avatars/ inside MEDIA_ROOT, which a profile is allowed to leave empty, so a profile created without one reads as empty.
  • An ImageField is a FileField that also checks the upload really is an image, which is why this project installs Pillow. The column holds a path, not the bytes.
  • Under manage.py test the tables are built straight from the models, so adding a field needs no migration for the tests to see it. The run command above does generate one, which is what the dev server needs.

2. The image on the page

  • The page renders exactly one <img> carrying id="avatar", in both cases below.
  • A profile with an avatar shows that file's own URL, which starts with /media/.
  • A profile without one shows /static/app/avatar-placeholder.svg, resolved through the static tag.
  • Reaching for .url on an empty field raises, so the branch is the only thing standing between an empty profile and a 500.

3. The URL follows the file

  • Replacing a profile's avatar changes what the page shows, because the src comes from the field rather than from anything written into the template.
  • No literal "/static/, '/static/, "/media/ or '/media/ string appears in the template: the avatar URL comes from the field and the placeholder URL from {% static %}.

Notes

  • Only app/models.py and app/templates/app/profile.html change.
  • Do not change MEDIA_ROOT, MEDIA_URL or the placeholder file.

More in Static & Media Files

See the module
Styled Pages With Static AssetsServe a stylesheet and a logo through Django's static files: the app static layout and the static template tag instead of hardcoded paths.100%3 of 3 solvedeasyDocument Upload HandlingAccept a validated PDF upload through a Django form and store it under MEDIA_ROOT, with the multipart encoding the browser needs.100%3 of 3 solvedeasyGallery Uploads PageBuild a photo gallery end to end: static assets through the static tag, validated image uploads into a dated media path, and a listing with a placeholder fallback.100%3 of 3 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.