carltongibson / django-template-partials

Reusable named inline partials for the Django Template Language.
MIT License
387 stars 15 forks source link

Integration with Django messages framework #43

Closed nanorepublica closed 2 months ago

nanorepublica commented 2 months ago

Before I detail this issue a couple of disclaimers on this one:

  1. If this is decided to be a good idea it is probably better done once it get's merged into Django core
  2. I am not 100% convinced on this as it requires a reference to something like HTMX which isn't baked into core and might never be.

With those two disclaimers aside, I picked up template-partials today to integrate send multiple responses to a custom admin page, one of them being a message.

Essentially I ended up with the following partial template:

{% partialdef messages %}
    {% if messages %}
    <ul hx-swap-oob="outerHTML:.messagelist" class="messagelist">
        {% for message in messages %}
        <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message.message|capfirst }}</li>
        {% endfor %}
    </ul>
    {% endif %}
{% endpartialdef %}

{% block messages %}
    <ul class="messagelist">
    {% partial messages %}
    </ul>
{% endblock messages %}

Which apart from the one HTMX attribute is pretty much exists right now so I figure this could be an easy win?

It also required this view code which I have seen as a middleware here

existing_messages = [
    {"message": message.message, "tags": message.tags}
    for message in messages.get_messages(request)
]

response_content += render_to_string(
    f"{template_name}#messages",
    {"messages": existing_messages},
)

I think the main blocker here is the specific HTMX code in the template.

nanorepublica commented 2 months ago

Thinking about this some more, this probably won't work as an integration since the partial definition and the declaration need to be in the same template file. This is more a ticket for the admin when it gets integrated into core.

carltongibson commented 2 months ago

Thanks for the report anyway @nanorepublica.

I think this related to #41. The problem is the same: you want to add extra oob parts onto the end of the response content.

As well as the strategies outlined there, a TemplateResponse subclass that knew to do the messages.get_messages() dance would stop this logic leaking into the view (but also not require making it middleware based).

It’s something I’m pondering but I think that’s something more view-layer appropriate (so Neapolitan) rather than template-partials itself (which is about the template tags only really).

nanorepublica commented 2 months ago

Yes you are correct that I took inspiration from #41 and thanks for the tip about TemplateResponse, I will look into that.

I think my general thought when opening this issue was what existing blocks could or would be more appropriate as a partial once template-partials is merged into core. As mentioned this thought is for other parts of core to possibly consider (admin, CBVs, etc) .

carltongibson commented 2 months ago

@nanorepublica Yep, gotcha.

Like you, I'd love to see us take up HTMX integration here, but I'm not sure exactly how to propose that, or what it might look like. (@jacobian had thoughts when we talked to him on Django Chat.)