Feature Request: New parameter for render_to_string() to render part of a template using a selector

660 views
Skip to first unread message

Dan Swain

unread,
Jul 15, 2022, 8:21:52 PM7/15/22
to django-d...@googlegroups.com

With the growing uptake of technologies like HTMX, I would like to see an additional parameter added to render_to_string().  In my rewritten definition of render_to_string() below (taken from the docs), I have named the parameter “part”.  In rendering portions of a template for responses to ajax calls, these template portions end up being stored in separate template snippet files and then {% include(d) %} in the main template.  I think that keeping everything related to a view in a single template file is much more desirable.  Therefore, just include part=selector in order to pull out the part of the template that is needed for rendering.  The file structure for managing templates would become much simpler with this approach.

 

render_to_string(template_name, part=None, context=Nonerequest=Noneusing=None)

render_to_string() loads a template like get_template() and calls its render() method immediately. It takes the following arguments.

template_name

The name of the template to load and render. If it’s a list of template names, Django uses select_template() instead of get_template() to find the template.

part

An optional CSS selector that will be used to cause only the portion of the template that matches the selector to be rendered.

context

dict to be used as the template’s context for rendering.

request

An optional HttpRequest that will be available during the template’s rendering process.

using

An optional template engine NAME. The search for the template will be restricted to that engine.

 

charettes

unread,
Jul 16, 2022, 10:45:38 AM7/16/22
to Django developers (Contributions to Django itself)
Don't you have to render the template in the first place if you want to extract a fragment of it? If that's the case then shouldn't render_to_string be left unchanged and libraries interested in this feature build something on top of it to achieve what they're after?

Given render_to_string is content type agnostic (I've seen it used to generate text, markdown, ...) and that safely dealing with CSS selectors and HTML parsing server side is large problem by itself I'm -1 on adding this feature as I don't see a compelling reason why this should be added to core.

I think you'd be much better served by a template backend that allows for developer defined logical sections of templates (e.g.  {% block %} like ) to be targeted for isolated rendering through provided context instead of a fully fledged HTML/CSS based solution that strips unused bytes from a fully rendered template. To me your proposed solution trades a lot of resources (HTML parsing, CSS selector sanitization and execution) for a promise of network usage reduction.

Cheers,
Simon

Ken Whitesell

unread,
Jul 16, 2022, 3:14:18 PM7/16/22
to django-d...@googlegroups.com
I'm going to chime in as a -1 here.

It does not seem to me that you can guarantee that you have the correct element(s) unless you actually render the complete template.

Once you've rendered that template, then you can use tools like BeautifulSoup to extract the elements desired and ignore the rest.

I don't see value in trying to replicate what BeautifulSoup provides within Django.

Ken
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/1c8201d898aa%240449fe50%240cddfaf0%24%40shenberger.org.


Adam Johnson

unread,
Jul 16, 2022, 4:38:18 PM7/16/22
to django-d...@googlegroups.com
I’m also -1.

There are many ways to “partially render” a template. I covered one in the Django-htmx docs mention one: 
https://django-htmx.readthedocs.io/en/latest/tips.html#partial-rendering . The proposal to render the full template and then throw away most of the result is a particularly wasteful one :)

I think it would be a better avenue to explore other ways of organising templates for different htmx patterns. I’d gladly consider documentation PR’s for django-htmx.

Dan Swain

unread,
Jun 12, 2023, 10:42:17 AM6/12/23
to Django developers (Contributions to Django itself)
Carlton Gibson has addressed my feature request:  https://github.com/carltongibson/django-template-partials
Reply all
Reply to author
Forward
0 new messages