Django : Is it a good idea to generate JS dynamically?

后端 未结 5 2155
北恋
北恋 2021-02-03 12:23

When I write my JS files for a Django project, of course I do some AJAX calls, and for the moment the urls for those calls are hard-coded (which is very ugly).

I was thi

5条回答
  •  半阙折子戏
    2021-02-03 12:54

    I searched deeper in those asset manager applications from djangopackages, have found out that django-mediagenerator provides that feature, even if it is not well documented : you can generate your js or css files as django templates, and then serve them statically (they are also bundled, and caching is managed etc ... so two birds with one stone + it is really easy to set-up !).

    In order to have JS files generated as django templates (after having set-up django-mediagenerator), just add the filter :

    ROOT_MEDIA_FILTERS = {
        'js': 'mediagenerator.filters.template.Template',
    }
    

    in your settings.

提交回复
热议问题