If the root issue is the perceived need to manually pass the local variables into the format_html call, you do have a couple options - the usefulness of either being really dependent upon how you’re using this in your views (or utility functions).
Put the variables in a dict and pass the dict as kwargs to the function. (eg. If all these variables are keys in a dictionary named variables, then you could call format_html(**variables)).
Depending upon just how much “control” you have over the string being formatted, it might be safe enough to call format_html(**locals()).
Either one of these can reduce the “parameter explosion” of large numbers of variables being interpolated or formatted in the function call.
No, that’s not true - in addition to the two methods I provided earlier, I’ve come up with at least two more. (Well, you might consider them variations on a theme rather than unique solutions.) If you’re doing this in a class, you could create a method that gathers local variables and then calls the format method, or you could write a wrapper function that takes the current class and builds the function call.
Like Ken suggested: format_html('<div>Hello {name}</div>', **locals()) – this is already as short as name=name and stays as short even when the number of arguments increases.
That question is one that will be answered once it is available. But even if not, there is nothing stopping you then from writing the code as you intend.
Curious, doesn’t that mean duplicating code/templates? Assume I have a list under /users and click on a user to edit (which should take me to /users/1/edit). I understand that with htmx you would make a fetch request, replace the content div and update the history. But if I manually go to /users/1/edit I’d like to see the full page. So I see two/three ways of doing that in a DRY way:
Use a template snippet and in the case of the full page request use {% include %} to get it and in the case of a htmx request render that directly
Use one template and {% extends template_variable %} to switch between solely between an “empty” base template and a template including the header/footer around the content
Render the full page either way (performance wise often not that much slower) and then use htmx to swap in only a matching css selector?
Since @apollo13 has answered this better than I would have been able to, I don’t have anything to add for the majority of your questions. However, there’s one comment you made that I would like to address:
<opinion> This doesn’t bother me in the least.
I consider linters and IDEs to be “support” tools, providing occasionally-useful information. But I don’t rely upon them to drive my code, nor do I let the information they provide affect how my code is designed or written.
If I’ve got a proven code design pattern that works for us in the context in which we’re using it, tools that don’t accept that pattern indicate a problem with that tool - not the code. <opinion>
An alternative approach is to use a library for generating HTML with python syntax. See hyperpython. I experimented with making my own such library, but don’t maintain it - see this example in its repo.