Template sets for widgets

110 views
Skip to first unread message

Gert Steyn

unread,
Feb 1, 2017, 6:05:08 AM2/1/17
to Django developers (Contributions to Django itself)
Hi All

I got very excited when I read about the upcoming template based widgets, it is something that is really needed. I would like to hear your comments on a proposal I have that should only be a couple of lines to add but would add a massive usability boost.

I propose that we add the concept of "template sets", that gives us the ability to switch between sets of widget templates on a global or per form level.

Taking Bootstrap as an example; you have a required structure for stacked forms another for inline forms and in v4 another for custom forms. The actual widget rendering for say a RadioSelect is different in all situations. You could manually change widget templates for each different situation etc, but...

Imagine if you could simply say:

class MyForm:
   
class Meta:
        template_set
= 'bootstrap_inline' # or 'admin', 'bootstrap_stacked', 'foundation', 'skeleton', 'uikit',..


and the required widget templates would automatically be used.

3rd party applications would quickly popup with template sets for Bootstrap, Foundation, Skeleton, UiKit etc and Django will automatically generate forms for that particular framework.

To implement this would be as easy as injecting an additional templates directory based on the value of template_set. Not setting template_set would mean the default template resolution applies.

I believe this would make Django truly framework agnostic and easy to implement any framework of your choosing or switch from one framework to another...

Your thoughts on 1) concept and 2) implementation?

Regards
Gert Steyn

Tim Graham

unread,
Feb 1, 2017, 9:28:44 AM2/1/17
to Django developers (Contributions to Django itself)
Hi Gert, I think the renderer API could be used for this use case. Did you look at that?

https://docs.djangoproject.com/en/dev/ref/forms/renderers/#the-low-level-render-api

Gert Steyn

unread,
Feb 1, 2017, 10:08:04 AM2/1/17
to Django developers (Contributions to Django itself)
Hi Tim

I did, and I agree that a TemplatesSetting renderer of sorts would most likely be the way to implement.

I also think that the use case of "Using some CSS framework" is common enough to warrant giving developers a clean/standard way of doing this rather than letting each roll their own. When last did you work on a Django project that didn't use a CSS framework?

Florian Apolloner

unread,
Feb 1, 2017, 2:08:48 PM2/1/17
to Django developers (Contributions to Django itself)
On Wednesday, February 1, 2017 at 3:28:44 PM UTC+1, Tim Graham wrote:
Hi Gert, I think the renderer API could be used for this use case. Did you look at that?

https://docs.djangoproject.com/en/dev/ref/forms/renderers/#the-low-level-render-api

What I currently miss here is that there is no real way to specify the renderer from the template. I personally would like to see something like:
{% form your_form renderer='some_thing' %}
or even:
{% form your_form prefix='bootstrap' %}

Maybe also the same for "{% field %}".  The latter would be especially cool since template authors could then just copy the existing templates into templates/bootstrap/ and change those. What do you think?

Gert Steyn

unread,
Feb 1, 2017, 3:20:59 PM2/1/17
to Django developers (Contributions to Django itself)
I think something as simple as

{% form your_form prefix='bootstrap' %}

and

{% field your_field prefix='bootstrap' %}

would add huge flexibility, exactly the concept I was trying to convey!

Reply all
Reply to author
Forward
0 new messages