Django Page Checklist

If you’ve never worked with Django, it’s worth going through the Django Girls Tutorial or the one on the Django Project website. If you’re trying to add a page to your Django application, and aren’t sure you’re doing it right, make sure you have the following things.

NOTE: I try to periodically update this article as new Django versions come out, but if you find that the links refer to an old version of Django, most of the Django docs site allows you to pick a different version using the “documentation version” menu in the bottom right corner.

  • The page will need a view function that will generate the page’s data and render a template with that data filled in.
    • The view function may take arguments from the URL pattern.
    • The view may need to query records from the database.
    • The view will need to raise appropriate errors if objects are not found. get_object_or_404 is a common shortcut for finding a single resource.
  • The view will need a template to render.
    • The template may need to inherit off a shared base template.
    • If it’s inheriting, the template will need to establish blocks to fill in the base template, and fill in any inherited material in the blocks with block.super.
    • Any static (CSS, JavaScript, image, etc.) files will need to have the correct static base URL in front of them, and the template (or one of its parent templates) will need to include the load static tag.
  • The app’s urlpatterns will need to have a listing for the page’s URL.
  • If the page is in a new app (which should be created with $ manage.py startapp), the app will need to be included in settings.INSTALLED_APPS and the app’s urls.py will need to be included in the main project urls.py.

NOTE: the steps relating to views assume function-based views. Some Django applications use class-based views instead. These are more advanced; don’t worry about using them when you’re first starting out. The other steps in this checklist will be the same regardless of what type of views you are using.

Changelog:

  • Updated 03 April 2021 to change to Django 3. (My thanks to the Django docs team for organizing their documentation well enough that it was easy to do this.)
  • Updated 10 June 2018 to change to Django 2.
Written on April 3, 2021