script type=module #358
-
Hi, Is there a way to set the type of the javascript script tag that is created? It looks like there is a add_module_attribute_to_scripts method in middleware.py but i can't see how its implemented. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
That method isn't currently used anywhere, and should probably be removed. To render the script tag for a specific component, the method The render_js() call returns the |
Beta Was this translation helpful? Give feedback.
-
I was hoping to be able to set it on a per component basis, I think the above code would add type=module to all script tags would it not? I was able to achieve what I needed without making any code changes after spending some time looking at the Django Media class It turns out since 4.1 the js file path can actually be an object that implements an html() method (https://docs.djangoproject.com/en/4.1/topics/forms/media/#paths-as-objects) That means you're able to directly specify how the script tag should look by doing something like this
Thanks for the quick reply though 😊 |
Beta Was this translation helpful? Give feedback.
-
You override the method I pointed to in your own specific component class. That means all usages of that class will get the type="module" thing, everything else unchanged. Happy to see that it's possible to solve with the built-in Media class, I was hoping that we would get some benefits down the line from it! :) |
Beta Was this translation helpful? Give feedback.
-
For anyone coming later to this discussion, this has been possible since 0.82. See [Customize how paths are rendered into HTML tags with media_class |
Beta Was this translation helpful? Give feedback.
For anyone coming later to this discussion, this has been possible since 0.82. See [Customize how paths are rendered into HTML tags with media_class
](Customize how paths are rendered into HTML tags with media_class
) and Paths as objects