Implementing HTMX in a Django app: Should I use two templates per view?
When trying to integrate HTMX into my Django app, I'm facing a design issue. My base.html file contains fixed layout elements (a navigation bar and a title). I created a container section with the ID #work-area, which needs to update dynamically when the user clicks a link in the navbar.
Here is the problem:
If I use a single partial HTML file: It works perfectly for HTMX requests, as it only loads the
#work-areacontent. However, if the user tries to access that specific internal URL directly via the browser, the page won't load the full layout (navbar and title will be missing).If I use two HTML files (one partial and one full): I can serve the partial template for HTMX requests and the full template (extending
base.html) for direct browser access. The issue is that this duplicates code and increases maintenance, as any changes must be applied to both files.
What solution or pattern represents the best practice for handling this scenario?
maybe show your HTML files. And how you use it.
if you have duplicated code then put it in separate function and execute it in both places - this way you have only one code.