Apologies, this is going to be long. There is an accepted ticket from 9 years ago, about renaming or removing Form.Media
.
I think it’s worth revisiting this issue, and ideally pushing it forward: Media
feels like a rather sad and neglected part of Django at the moment. It’s not part of all the recent template-based form improvements, as far as I can tell. It’s fairly complex for what it tries to do, especially with the magic inheritance by way of metaprogramming: if you inherit from a class that has a class Media
, and your new inherited class also has a class Media
, the two get magically merged (which is the only reason all Widget
subclasses have a metaclass, incidentally).
The feature has also not kept up with changes in front-end development. There is no way to define that a provided script should be loaded with async
/defer
, which is a very common use case. We don’t support nonces or integrity attributes for security. On the docs side, we list deprecated media types like tty
, tv
aural
etc, some of which have been deprecated for a while, which also highlights how odd it is that this part of Django should care about the W3C’s media query updates. (Side note: this is really only a docs issue, as Django does not validate those keys.)
I wish we could get rid of it entirely, but I suspect we really should keep it or something like it around for the admin (which means keeping around all the merge logic in metaclasses etc, which isn’t ideal, but at least a compromise?). The whole thing feels just very out of place in Django – I can see that it’s a janky-but-useful way of extending the admin, but for regular form.
To play devil’s advocate: The feature is not entirely unused – GitHub finds 8.7k results, 4.7k of which appear in some sort of admin. It’s also in django-CSM, though I didn’t look into it in detail to see how recommended or used it is there.
As far as I can see, sensible options are:
- Deprecate and remove
Media
entirely and have some replacement in admin to allow people to add scripts and styles - Deprecated and remove
Media
fromForm
, but keep it in admin sites (not much maintenance advantages here) - Add
{{ form.media }}
to default form templates – and maybe change it so that we support things likedefer
andasync
in thejs
attribute.
I’m posting this here in the hopes of hearing about general use of Form.Media
and maybe finding a consensus on how to move forward.