Move all custom form rendering to form templates #6905

Closed
opened 2025-12-29 19:46:32 +01:00 by adam · 2 comments
Owner

Originally created by @jeremystretch on GitHub (Aug 29, 2022).

Proposed Changes

There are several places within NetBox where we define non-standard HTML templates for views in order to customize how forms are rendered. We should move all such logic into form-specific templates.

Justification

Django 4.0 introduced template-based form rendering, which enables the creation of a template for a specific form without having to override the template for the view in which it's rendered. This should help clean up the template code a bit.

Originally created by @jeremystretch on GitHub (Aug 29, 2022). ### Proposed Changes There are several places within NetBox where we define non-standard HTML templates for views in order to customize how forms are rendered. We should move all such logic into form-specific templates. ### Justification Django 4.0 [introduced](https://docs.djangoproject.com/en/4.1/releases/4.0/#template-based-form-rendering) [template-based form rendering](https://docs.djangoproject.com/en/stable/ref/forms/api/#django.forms.Form.render), which enables the creation of a template for a specific form without having to override the template for the view in which it's rendered. This should help clean up the template code a bit.
adam added the type: housekeeping label 2025-12-29 19:46:32 +01:00
adam closed this issue 2025-12-29 19:46:32 +01:00
Author
Owner

@jeremystretch commented on GitHub (Feb 18, 2023):

I did some digging into this today, and unfortunately it doesn't seem as straightforward to implement as I had hoped. There are two template renderers that might interest us: DjangoTemplates and TemplatesSetting.

DjangoTemplates is the default renderer, and loads templates from two places:

  • The built-in Django templates path
  • A templates/ path within each installed app

This seems suboptimal for us, as we store most templates under the project-level templates/ directory (rather than inside each app), and I much prefer this approach as it makes organizing common templates and searching for template code much easier.

The TemplatesSetting renderer loads templates per the TEMPLATES setting used to load non-form templates. This would work for us, except that it excludes the built-in Django path entirely, so forms and widgets that rely on these stock templates break. We could add the Django forms template path to TEMPLATES, but this seems a bit messy.

Further, it's disconcerting that there are two base renderer attributes that were both introduced and deprecated in Django 4.1.

Given the unexpected challenges in implementing the proposed change, I'm going to bump this from v3.5 and leave it open for discussion.

@jeremystretch commented on GitHub (Feb 18, 2023): I did some digging into this today, and unfortunately it doesn't seem as straightforward to implement as I had hoped. There are two template renderers that might interest us: [`DjangoTemplates`](https://docs.djangoproject.com/en/4.1/ref/forms/renderers/#djangotemplates) and [`TemplatesSetting`](https://docs.djangoproject.com/en/4.1/ref/forms/renderers/#templatessetting). `DjangoTemplates` is the default renderer, and loads templates from two places: * The built-in Django templates path * A `templates/` path within each installed app This seems suboptimal for us, as we store most templates under the project-level `templates/` directory (rather than inside each app), and I much prefer this approach as it makes organizing common templates and searching for template code much easier. The `TemplatesSetting` renderer loads templates per the `TEMPLATES` setting used to load non-form templates. This would work for us, except that it excludes the built-in Django path entirely, so forms and widgets that rely on these stock templates break. We could add the Django forms template path to `TEMPLATES`, but this seems a bit messy. Further, it's disconcerting that there are two [base renderer](https://docs.djangoproject.com/en/4.1/ref/forms/renderers/#django.forms.renderers.BaseRenderer) attributes that were both introduced and deprecated in Django 4.1. Given the unexpected challenges in implementing the proposed change, I'm going to bump this from v3.5 and leave it open for discussion.
Author
Owner

@jeremystretch commented on GitHub (May 2, 2023):

Closing this as there's no real impetus for its implementation. Happy to revisit this in the future, perhaps as part of the UI overhaul for v4.0.

@jeremystretch commented on GitHub (May 2, 2023): Closing this as there's no real impetus for its implementation. Happy to revisit this in the future, perhaps as part of the UI overhaul for v4.0.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6905