Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crispy should render form.media after the form as default #1331

Open
nerdoc opened this issue Jan 27, 2023 · 1 comment
Open

crispy should render form.media after the form as default #1331

nerdoc opened this issue Jan 27, 2023 · 1 comment

Comments

@nerdoc
Copy link

nerdoc commented Jan 27, 2023

  • Package version: django-crispy-forms 1.14.0, crispy-bootstrap5 0.7
  • Django version: 4.1
  • Python version: 3.10
  • Template pack: bootstrap5

Description:

Crispy renders the form's Media before the form in the HTML. is this correct and helpful?

I can mostly imagine cases where first the HTML tags are added in the DOM (e.g. via HTMX), and afterwords the js code should be executed, because it searches the DOM for some ids or classes and puts some event listeners onto them.

Demo code (simplified):

<button class=" decrease-time-button"> + </button>
<input type="time" id="{{ field.id_for_label }}" step="{{ step }}" data-units="{{ units }}" value="{{ field.value }}" class="form-control"/>
<button class="increase-time-button" type="button"> - </button>
document.querySelectorAll(".increase-time-button").forEach((element) =>  {
    element.addEventListener('click', increaseTime);
  })

In the default setting, where crispy renders Media before the form, document.querySelectorAll(".increase-time-button") finds nothing.
When I write

{% crispy form %}
{{ form.media }}

everything works as intended.

So I suppose it would be better to put it after the form.

Does anything speak against that?

@nerdoc nerdoc changed the title crispy should render Media **after** the form crispy should render Media after the form Jan 27, 2023
@nerdoc nerdoc changed the title crispy should render Media after the form crispy should render form.media after the form as default Jan 27, 2023
@smithdc1
Copy link
Member

Does anything speak against that?

Mainly the fact that it's been like this for a quite some time, it was originally introduced 9 years ago.

560171a

I know very little about JavaScript so I can't really comment on one approach over the other. However, the fact that it's been like that for this long makes me believe folk will be relying on the current configuration.

The form template is fairly small, so one suggestion could be to use a custom template and set it using the template attribute on the FormHelper.
https://django-crispy-forms.readthedocs.io/en/latest/form_helper.html#helper-attributes-you-can-set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants