Skip to main content

What to learn next

This tutorial gave you a quick introduction to a lot of tools. Here’s where to go to learn more.

More Django

We only used a small slice of Django.

  • Work through the official Django tutorial. It covers models, views, templates, the admin and testing.
  • Learn more about forms. Forms do a lot of work in a Django app: validation, error messages and rendering.

Go deeper with each tool

Every tool in this tutorial has good official docs. Now that you know the basics, they’ll be much easier to read.

Organise your templates

As an app grows, templates get messy. Two tools help:

  • Template partials let you define a small, named piece of a template and render it on its own. That suits HTMX, which often needs only one piece of a page. They’re built into Django from version 6.0.
  • Django Cotton lets you build reusable components, like buttons and cards, and use them like HTML tags.

Test smarter

We used Playwright for our tests. Playwright tests are great, but they’re slow, because they run a real browser.

  • Learn pytest. Our Playwright tests already run on pytest, so it makes sense to use it for all your tests. Add pytest-django so pytest can talk to your Django database. Django comes with its own testing tools built on Python’s unittest, but mixing the two styles in one project gets confusing, so stick with pytest.
  • Learn when to use which kind of test. Use unit tests for logic that doesn’t need a browser, like models, forms and views. Save Playwright for checking that the pieces work together in the browser.
  • Use factory_boy to create test data. It saves you from writing the same setup code in every test.
  • Try test-driven development (TDD). In this tutorial, we wrote our tests at the end. That’s better than nothing, but tests are more valuable when you write them up front. With TDD, you write a failing test first, then write just enough code to make it pass, then tidy up. Writing the test first makes you think about what the feature should do before you think about how to build it. Obey the Testing Goat teaches TDD with Django, and it’s free to read online.

Get more out of Playwright

We only used a few of Playwright’s features. Here are some more that are worth trying on the tests you already have.

Traces

A trace is a recording of a whole test run. Run your tests with tracing on:

uv run pytest --tracing on

Playwright saves a trace.zip file for each test in the test-results folder. Open one:

uv run playwright show-trace test-results/<test-name>/trace.zip

The trace viewer lets you step through the test one action at a time. For each step, you see a snapshot of the page before and after, the locator it used, the network requests and the console messages. You can see the HTMX request go out and the new list come back.

Use --tracing retain-on-failure to keep traces only for tests that fail. When a test fails on someone else’s machine, or on a CI (Continuous Integration) server, the trace shows you what happened.

Screenshots and videos

Playwright can save a picture of the page when a test fails:

uv run pytest --screenshot only-on-failure

It can also record a video of every test:

uv run pytest --video on

Both land in the test-results folder. You can also take a screenshot from inside a test with page.screenshot(). See the pytest plugin options for the full list.

Pause a test and look around

Add page.pause() anywhere in a test, then run it headed:

uv run pytest --headed

The test stops at that line and opens the Playwright Inspector, the same window you used with codegen. You can try out locators on the live page, then step through the rest of the test.

Other browsers

Our tests ran in Chromium. Run them in Firefox and WebKit (the engine behind Safari) too:

uv run pytest --browser chromium --browser firefox --browser webkit

If a browser is missing, install it with uv run playwright install.

Different screen sizes

Tailwind lets you give small screens a different layout, so it’s worth testing small screens too. Run your tests as if they were on a phone:

uv run pytest --device "iPhone 13"

This sets the screen size, the user agent and touch support to match that phone. Playwright knows a long list of devices.

To test one exact size inside a test, use page.set_viewport_size():

def test_list_fits_on_a_small_screen(page: Page, live_server):
    page.set_viewport_size({"width": 375, "height": 667})
    page.goto(live_server.url)
    expect(page.get_by_role("button", name="Add")).to_be_visible()

Combine this with screenshots to see what each size looks like. See emulation for what else you can fake, like the time zone, the language and dark mode.

Fake the server’s response

page.route() lets a test catch requests before they reach the server, and send back whatever you like. You can test what your page does when the server is slow, sends an error, or is down, without breaking your real server.

Check the structure of the page

An ARIA snapshot describes a part of the page the way a screen reader sees it: headings, checkboxes, buttons and their text. expect(locator).to_match_aria_snapshot(...) checks a whole section in one go, and codegen’s Assert snapshot button writes one for you. It also tells you how usable your page is for someone using a screen reader.

Add authentication

Real apps need users to sign up and log in. django-allauth handles sign up, log in, password resets, social logins (like “Log in with GitHub”) and more.

Learn Git

If you’re not yet confident with Git, it’s really worth learning, even in the age of AI.

AI tools can write code fast, and they make mistakes fast too. Git lets you see exactly what changed, undo it, and try things safely on a branch. It’s also how teams work together.