Package: django-asset-definitions
django-asset-definitionsAsset definitions are collections of static files (JavaScript, CSS) defined in Python code and (re)used in Django views
and templates.An example of asset definition isMediaclass that you candefine in Django forms.django-asset-definitionsaim to extend the application of asset definitions beyond forms and use them as the main
way to describe and organize JavaScript and CSS files.Installationpipinstalldjango-asset-definitionsUsageDefine:importasset_definitionsmy_media=asset_definitions.Media(js=("media.js","""<script>window.addEventListener("load", function() {console.log("Loaded!");});</script>""",),css={"all":("media.css",),})Combine:extended_media=my_media+asset_definitions.Media(js=("extension.js",))Define in views:importasset_definitionsclassMyView(asset_definitions.MediaDefiningView,...):classMedia:js=("media.js",)css={"all":("media.css",),}...Form media is added to view media automatically:importasset_definitionsfromdjango.views.genericimportFormViewclassMyFormView(asset_definitions.MediaDefiningView,FormView):...Use in templates:{{view.media.render}}Or:{{view.media.js.render}}{{view.media.css.render}}See an extended example below.asset_definitions.Mediaanddjango.forms.Mediaasset_definitions.Mediaprovides the same API asdjango.forms.Media. In fact, it is inherited fromdjango.forms.Media.It is safe to combineasset_definitions.Mediawithdjango.forms.Media.asset_definitions.Mediaobjects are lazy. If two or more instances ofasset_definitions.Mediaare combined,
the result is computed only when media is rendered. It is safe to usereverse_lazy()withasset_definitions.Media. It is important if you define your assets on a module level.Mediaclass inMediaDefiningViewdoes not supportextendoption. To add to the media defined in a parent
view class, you should overrideget_mediamethod and usesuper(MyView,self).get_media().Example:myapp/urls.py:urlpatterns=(url("/",MyView.as_view()),url("/global-variables.js",global_js_variables,name="global_js_variables"),)myapp/views.py:importasset_definitionsfrom.importassetsclassMyView(assets_definition.MediaDefiningView,TemplateView):template_name="template.html"classMedia:js=("media.js",)css={"all":("media.css",)}defget_media():return(assets.global_js_variables+assets.jquery+super(MyView,self).get_media())defglobal_js_variables(request):js_content='const CURRENT_USER="{}";'.format(request.user)returnHttpResponse(js_content,content_type="application/javascript")myapp/assets.py:importasset_definitionsfromdjango.coreimporturlresolversglobal_js_variables=asset_definition.Media(js=urlresolvers.reverse_lazy("global_js_variables"),)jquery=asset_definitions.Media(js="jquery.js")myapp/templates/template.html:<html>
  <head>{{view.media.css.render}}</head>
  <body>
    ...{{view.media.js.render}}</body>
</html>Changes1.0.0Add Django 3.x and 4.x compatibilityDrop support for Python 2Python 3.6 is the minimum requirement0.3Add Django 2.0 compatibilityExtractMediaDefiningClassbase class fromMediaDefiningView0.2Specify different requirements insetup.pybased on the current Python versionAllow using inline JavaScript in media definitions (wrap with<script>tag)0.1Initial release