Answer the question
In order to leave comments, you need to log in
Best practices for working with javascript?
Let's say I have a repeating block of code that I place in several places on the site. It has a Javascript handler attached to it. There is such a problem - how to actually connect it correctly?
Let's say I include a block with {% include 'some_block.html' %}. Every time remember that it is necessary to separately include the appropriate code with Javascript in the template? Not very flexible...
I solve part of the problem with django-embedded-media. But maybe there is another way?
Another problem is that in the script file I call some url with Ajax. In templates, it is convenient to write a link through {% url 'some_url' param=some_value %}, but what about script files? Hardcode? - Sloppy somehow ... I want a better solution.
How is it generally accepted to look for a way out in these situations?
How is it organized in the "best houses in Paris"?
Answer the question
In order to leave comments, you need to log in
The first problem is solved by using django-sekizai . In some_block.html include code
{% addtoblock "js" %}
<script type="text/javascript" src="{% static 'js/some_script.js' %}"></script>
</script>
{% endaddtoblock %}
<script type="text/javascript">
window.urls = {
some_url: {% url 'some_url' param=some_value %}
}
</script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question