J
J
JonGalt2017-04-05 11:19:25
Django
JonGalt, 2017-04-05 11:19:25

How to load a script with Ajax in Django?

Question 1
In the base template, Django made 2 functions for Ajax requests

function ajax_get(url, block_id) {
             $.ajax({
                 url: url,
                 type: 'GET',
                 dataType: 'html',
                 success: function (data) {
                     $(block_id).html(data);
                 }
             })
         }

        function ajax_post(url, form_id, block_id) {
            var form = $(form_id).serialize();
                $.ajax({
                    url: url,
                    type: 'POST',
                    data: form,
                    dataType: 'html',
                    success: function (data) {
                        $(block_id).html(data);
                    }
                });
                return false;
        }

And in the future, I just use these functions to load templates instead of writing my own function for each request. If I put these functions in a separate ajax.js file and connect it, then for some reason they do not work. Why?
And Question 2
One of the templates has a script that loads another script

$( document ).ready(function() {
        loadScript("{% static 'js/plugin/bootstraptree/bootstrap-tree.min.js' %}");
    });
</script>

It works on the first boot, but then it doesn't.
How to fix it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question