Summary

This brings a close to the core site admin functionality of the project. It’s clear to see how it’s possible to tailor what buttons and levers you wish to include, and build up a useful set of tools for total control over your application.

The pattern is the same throughout: an API function does the work, a view checks the user is a superuser and calls it, and a template shows the result. Every view that changes something is a POST, and checks the user again, because a form can be sent by anyone.

The example code includes test_admin.py, which checks that only superusers can see the admin pages, updating users, approving comments and unpublishing posts, that administrators can’t lock themselves out, and that deleting a user keeps their posts.

Code

The example code at this point is tagged ch09-site-admin in the example repository. To get it:

git clone https://github.com/codewalkr/fastapi-example-for-you.git
cd fastapi-example-for-you
git checkout ch09-site-admin