Django template call javascript function on loop

I am looping in a javascript template, for example:

{% for movie in movies %}
    {{movie.name}}
{% endfor %}

Be that as it may, I can call a javascript function that returns the required DOM element, for example:

{% for movie in movies %}
    <script>
        function get_movie(name) {
            return "<div> class='movie-title'>name</div>
        }
        get_movie({{movie.name}})
    </script>
{% endfor %}

I just want to call the js function and check the check and return the element according to ..

+4
source share
1 answer

Of course it is possible. You are better off moving the tag <script>from the django loop and may also function. For reference, I will give an example from my code that draws diagrams on my django admin page:

<script type="text/javascript">
    {% if cl.show_chart %}
        (function($) {
            $(document).ready(function() {
                var data = [
                    {% for sold in cl.get_sold_info %}
                        {
                            fullname: '{{ sold.fullname }}',
                            date: {{ sold.date|date:"U" }}000,
                            partner: '{{ sold.partner }}',
                            price: {{ sold.price }}
                        },
                    {% endfor %} ];
                draw_charts(data, $);
            });
        })(someNamespace.jQuery);
    {% endif %}
</script>

, <script> - django, for-loop . draw_charts - . - , , , PHP4.

+1

Source: https://habr.com/ru/post/1671214/


All Articles