Provide forms field/widget type information for use in templates

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Provide forms field/widget type information for use in templates

Paweł Adamczak
Hiya,

I would like to 'resurrect' ticket #13009, or at least discuss it.

I think that the suggested feature would be beneficial, as it comes up every time I'm implementing HTML flats different types of fields require different rendering (the most common example being checkboxes, where the input field is before the label). Adding an easy, builtin method of checking the field type/widget would be much nicer in my opinion then using then using  - just for example - this approach.

That question also comes up in couple of StackOverflow questions:
- http://stackoverflow.com/questions/3927018/django-how-to-check-if-field-widget-is-checkbox-in-the-template (viewed 7594 times)
- http://stackoverflow.com/questions/1809874/get-type-of-django-form-widget-from-within-template (viewed 15015 times)
- http://stackoverflow.com/questions/20424375/django-templates-how-to-know-form-field-type-and-add-any-buttons-based-on-field (viewed 3633 times)

From the ticket:
Broadly the idea has merit, but it needs a bit of finesse - specifically, what determines the "type" of the field? Do we just use the type of the input element on the widget? What do we do in the case of multi-widgets?
 
As far as I understand Django tries to structure its widget structure based on the HTML spec, and I would follow that convention as close as possible, with widget 'types' (the nomenclature would probably need to be different) like 'input_text', 'input_email', ..., 'input_radio', 'input_checkbox', and 'select'. In other cases we would just mimic the widget name ('multi_widget'). 

What do you think?
I would be happy to contribute to Django and implement the above in a feature pull request.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/02725db5-3873-4651-9f2e-01c7b2af9f85%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Provide forms field/widget type information for use in templates

Collin Anderson-2
Hi,

If it helps It looks like there's a widget.input_type attribute, so you can at least do {% if theform.thefield.field.widget.input_type == "checkbox" %} etc. 

Collin


On Mon, Mar 20, 2017 at 9:46 AM, Paweł Adamczak <[hidden email]> wrote:
Hiya,

I would like to 'resurrect' ticket #13009, or at least discuss it.

I think that the suggested feature would be beneficial, as it comes up every time I'm implementing HTML flats different types of fields require different rendering (the most common example being checkboxes, where the input field is before the label). Adding an easy, builtin method of checking the field type/widget would be much nicer in my opinion then using then using  - just for example - this approach.


From the ticket:
Broadly the idea has merit, but it needs a bit of finesse - specifically, what determines the "type" of the field? Do we just use the type of the input element on the widget? What do we do in the case of multi-widgets?
 
As far as I understand Django tries to structure its widget structure based on the HTML spec, and I would follow that convention as close as possible, with widget 'types' (the nomenclature would probably need to be different) like 'input_text', 'input_email', ..., 'input_radio', 'input_checkbox', and 'select'. In other cases we would just mimic the widget name ('multi_widget'). 

What do you think?
I would be happy to contribute to Django and implement the above in a feature pull request.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/02725db5-3873-4651-9f2e-01c7b2af9f85%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAFO84S4wndx1sKYf9hnQsytCZeLgjGBeJZJM94xr-SJazZx-4Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Provide forms field/widget type information for use in templates

Paweł Adamczak
Huh, don't know how I missed that. Thanks!

In that case, I would add the information to the docs, to the ticket, and close it : -)

On Monday, March 20, 2017 at 4:36:17 PM UTC, Collin Anderson wrote:
Hi,

If it helps It looks like there's a widget.input_type attribute, so you can at least do {% if theform.thefield.field.widget.input_type == "checkbox" %} etc. 

Collin


On Mon, Mar 20, 2017 at 9:46 AM, Paweł Adamczak <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="u7HMv46ECAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">pawe...@...> wrote:
Hiya,

I would like to 'resurrect' ticket <a href="https://code.djangoproject.com/ticket/13009" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fcode.djangoproject.com%2Fticket%2F13009\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFuHraFcfYCPGg8Mi7LcbCHiB8VOQ&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fcode.djangoproject.com%2Fticket%2F13009\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFuHraFcfYCPGg8Mi7LcbCHiB8VOQ&#39;;return true;">#13009, or at least discuss it.

I think that the suggested feature would be beneficial, as it comes up every time I'm implementing HTML flats different types of fields require different rendering (the most common example being checkboxes, where the input field is before the label). Adding an easy, builtin method of checking the field type/widget would be much nicer in my opinion then using then using  - just for example - <a href="http://stackoverflow.com/a/14291933/3023841" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fa%2F14291933%2F3023841\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGOAyf9mJFuHPsOeOxvnzaoXUNymA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fa%2F14291933%2F3023841\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGOAyf9mJFuHPsOeOxvnzaoXUNymA&#39;;return true;">this approach.

That question also comes up in couple of StackOverflow questions:
- <a href="http://stackoverflow.com/questions/3927018/django-how-to-check-if-field-widget-is-checkbox-in-the-template" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F3927018%2Fdjango-how-to-check-if-field-widget-is-checkbox-in-the-template\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF6SiyzDoPwV6ERVvM_BwMW5VXqnA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F3927018%2Fdjango-how-to-check-if-field-widget-is-checkbox-in-the-template\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNF6SiyzDoPwV6ERVvM_BwMW5VXqnA&#39;;return true;">http://stackoverflow.com/questions/3927018/django-how-to-check-if-field-widget-is-checkbox-in-the-template (viewed 7594 times)
- <a href="http://stackoverflow.com/questions/1809874/get-type-of-django-form-widget-from-within-template" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F1809874%2Fget-type-of-django-form-widget-from-within-template\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH9BF3RHR5TQ8eUndf698c8WS-dJQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F1809874%2Fget-type-of-django-form-widget-from-within-template\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH9BF3RHR5TQ8eUndf698c8WS-dJQ&#39;;return true;">http://stackoverflow.com/questions/1809874/get-type-of-django-form-widget-from-within-template (viewed 15015 times)
- <a href="http://stackoverflow.com/questions/20424375/django-templates-how-to-know-form-field-type-and-add-any-buttons-based-on-field" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F20424375%2Fdjango-templates-how-to-know-form-field-type-and-add-any-buttons-based-on-field\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNE0g38lsJBO4ygRdDg9ZuWQNWXuyw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F20424375%2Fdjango-templates-how-to-know-form-field-type-and-add-any-buttons-based-on-field\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNE0g38lsJBO4ygRdDg9ZuWQNWXuyw&#39;;return true;">http://stackoverflow.com/questions/20424375/django-templates-how-to-know-form-field-type-and-add-any-buttons-based-on-field (viewed 3633 times)

From the ticket:
Broadly the idea has merit, but it needs a bit of finesse - specifically, what determines the "type" of the field? Do we just use the type of the input element on the widget? What do we do in the case of multi-widgets?
 
As far as I understand Django tries to structure its <a href="https://docs.djangoproject.com/en/1.10/ref/forms/widgets/#widgets-handling-input-of-text" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2F1.10%2Fref%2Fforms%2Fwidgets%2F%23widgets-handling-input-of-text\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHekW6b1WJ2cyWEqLPSIaFcSO0myg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdocs.djangoproject.com%2Fen%2F1.10%2Fref%2Fforms%2Fwidgets%2F%23widgets-handling-input-of-text\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHekW6b1WJ2cyWEqLPSIaFcSO0myg&#39;;return true;">widget structure based on the <a href="https://www.w3schools.com/tags/att_input_type.asp" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwww.w3schools.com%2Ftags%2Fatt_input_type.asp\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFDA3fPDo8vV2ory9hP94STtSpSdA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwww.w3schools.com%2Ftags%2Fatt_input_type.asp\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFDA3fPDo8vV2ory9hP94STtSpSdA&#39;;return true;">HTML spec, and I would follow that convention as close as possible, with widget 'types' (the nomenclature would probably need to be different) like 'input_text', 'input_email', ..., 'input_radio', 'input_checkbox', and 'select'. In other cases we would just mimic the widget name ('multi_widget'). 

What do you think?
I would be happy to contribute to Django and implement the above in a feature pull request.

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="u7HMv46ECAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-develop...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="u7HMv46ECAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-d...@googlegroups.com.
Visit this group at <a href="https://groups.google.com/group/django-developers" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/django-developers&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/django-developers&#39;;return true;">https://groups.google.com/group/django-developers.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/django-developers/02725db5-3873-4651-9f2e-01c7b2af9f85%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-developers/02725db5-3873-4651-9f2e-01c7b2af9f85%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-developers/02725db5-3873-4651-9f2e-01c7b2af9f85%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/django-developers/02725db5-3873-4651-9f2e-01c7b2af9f85%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/1301a519-acba-4d6f-a619-5da7db176d47%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.