0👍
Well, here’s an example.
# views.py
class SomeView(View):
def get(self, request, *args, **kwargs):
context = {} # put some data here
return render(request, "pathto/jsfile.js", context)
# urls.py
urlpatterns = [
# ...
path("chart/js", views.SomeView.as_view(), name="js"),
# ...
]
# template
<!-- example -->
<div>
<canvas id="chart"></canvas>
<script type="text/javascript" src="{% url 'js' %}"></script>
<div/>
# jsfile.js
var chart = document.getElementById("chart");
{% if some_context %}
// do something
{% endif %}
Source:stackexchange.com