Answer the question
In order to leave comments, you need to log in
How to correctly organize the use of JS in Django templates?
There is a template for dzhanga, which uses JS quite actively (a template for 300 lines, 200 of them are JS).
As I understand it, it would be nice to put the JS in a separate file, but some variables in the JS code are initialized by the django template engine, for example: var currentTopicId = {{ topics.first.pk }};
In addition, there are DOM objects obtained by ID: var topicTitleElement = $('#topic-title');
If I put the code in a JS file, but leave initialization currentTopicId
in the template, then PyCharm will swear at an unknown variable (there shouldn’t be an error in runtime, but it’s uncomfortable to work). Also, PyCharm won't know the ID of the DOM elements I'm looking for (same #topic-title
). In general, it will not be very convenient to work.
Maybe there are some nicer ways to do it?
Answer the question
In order to leave comments, you need to log in
<body data-attrib="{{ some_var }}">
and $(document).ready(...)
enough to solve problems
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question