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

Styled Pages With Static Assets

easyDjango Static FilesTemplates100%3 of 3 solved

Problem statement

The catalogue page asks the browser for /assets/site.css and /assets/logo.svg. Nothing serves either one, so the page arrives unstyled with a broken image - and even if those files existed at that path today, the first time the app is mounted somewhere other than the site root, or STATIC_URL changes, every reference breaks again. Django has a system for this: the app keeps its own assets, and the template asks Django for their URLs instead of guessing.

Requirements

Implement:

  • move assets/site.css and assets/logo.svg into the app's own static directory, app/static/app/
  • the asset URLs in app/templates/app/catalog.html

The following files are already implemented and provided: app/models.py, app/views.py and app/urls.py. GET /catalog/ renders app/templates/app/catalog.html with the product list. django.contrib.staticfiles is installed and STATIC_URL is /static/ in project/settings.py. The finished stylesheet and logo are provided in the top-level assets/ folder (assets/site.css and assets/logo.svg), but nothing serves that folder: the template currently asks the browser for /assets/site.css and /assets/logo.svg, and both fail. Nothing under app/ holds any assets yet.

1. The app's own static directory

  • Move assets/site.css to app/static/app/site.css (create the static/app/ folders inside app/). It is then found by the staticfiles finders as app/site.css. Keep its contents; you do not need to write any CSS.
  • Move assets/logo.svg to app/static/app/logo.svg the same way. It is then found as app/logo.svg.
  • The nesting is the point: everything inside an app's static/ directory is collected into one shared namespace, so an app keeps its files under a folder named after itself and no other app can collide with it.

2. The rendered catalogue page

  • The page links the stylesheet as href="/static/app/site.css" and shows the logo as src="/static/app/logo.svg".
  • Both URLs are served: GET /static/app/site.css and GET /static/app/logo.svg each answer 200 with the file.

3. No typed asset path left in the template

  • The template loads the static tag library with {% load static %} before its first {% static %} tag, and resolves both assets through that tag.
  • No literal /assets/, "/static/ or '/static/ string survives anywhere in the template source. Every asset URL comes from the tag, so it keeps saying the right thing when the site moves or STATIC_URL changes, which a typed path cannot do.
  • The existing id="logo" on the image and the product loop stay as they are.

Notes

  • Only app/templates/app/catalog.html changes, plus moving the two files from assets/ into app/static/app/.
  • Do not edit STATIC_URL or add STATICFILES_DIRS; the app's own static directory is the whole mechanism here.
  • Do not add a build step, a CDN or any package.

More in Static & Media Files

See the module
Document 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 solvedeasyAvatar Image FieldAdd an image field with an upload-to path and render the stored image's URL, falling back to a static placeholder.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.