Hi.
Probably this is an old subject, and it's been argued lots of times, but I guess I need some help to find the right resources.
When building a real-world django application, you need javascript. Lots of it. Making JS talk to django is more or less easy: $.ajax(), django-rest-framework, and you're done. The problem comes when you need Django talking to JS. You need this variable initialized with some View context value, or init the i18n of that plugin with the {{ LANGUAGE_CODE }}, or whatever. There are a thousand cases that are on everybody's mind.
The obvious way is to add a <script> tag in your template, and mix JS with django templates. Some examples:
$('{{ form.my_field.id_for_label}}').init_plugin();
var error_message = "{% trans 'You have a problem' %}";
var url = "{% url 'some:django:url' %}"
{% for item in some_context_variable %}
do_something();
{% endfor %}
You get the idea. But it looks like a terrible anti-pattern. You can't reuse the JS code between templates, you can't consolidate all the JS in one big cacheable file, you can't minimize the JS code, you can't just serve the files as static content from a CDN...
Is there any recommended way of approaching this kind of problems? It seems like a very obvious deployment-production subject to me, but I can't find any good practices documentation on this.
Thank you!