HTML-on-the-server

This guide focuses largely on building API backends. However, many real-world Django applications use the tried and tested approach of generating HTML on the server using Django's template system. Combined with a lightweight frontend enhancement library like htmx, this pattern can often have massive productivity benefits over more heavyweight browser-based applications built with libraries such as React.

The approaches described so far map quite well onto this style of application. It's still a good idea to divide views up into those that handle GET requests and those that handle POST requests, although POST handlers do also need to handle the initial GET request for the form too, in order for validation errors to work correctly.

As already discussed, it's a good idea to follow the suggestions in Django Views - The Right Way by Luke Plant.

For GET views, I'd suggest using django-readers to fetch data first, passing simple dictionaries of values (rather than model instances) into your templates, and using django-zen-queries to ensure that no unexpected database queries are executed during template rendering.

For POST handlers, use Django forms to validate incoming data, then a separate layer of action functions to encapsulate business logic.

Summary

The patterns discussed in this guide work just as well for HTML-on-the-server views as they do for API endpoints.