Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: $(…).textcomplete is not a function inside Django

I’m trying to implement $(‘textarea-selector’).textcomplete method from https://github.com/ranvis/jquery-textcomplete/blob/master/jquery.textcomplete.js in Djnago Textarea widget.

django/forms/templates/django/forms/widgets/textarea.html

{% load static %}

<textarea name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
{% if widget.value %}{{ widget.value }}{% endif %}</textarea>

<script src="js/tasks/jquery-1.10.2.min.js"></script>

<script src="js/tasks/jquery.textcomplete.min.js"></script>
<script type='text/javascript'>
    $('#id_{{ widget.name }}').textcomplete([
    {
        words: ['select', 'from', 'fct_table1', 'fct_table2', 'where', 'column1', 'column2'],
        match: /\b(\w{2,})$/,
        search: function (term, callback) {
            callback($.map(this.words, function (word) {
                return word.indexOf(term) === 0 ? word : null;
            }));
        },
        index: 1,
        replace: function (word) {
            return word + ' ';
        }
    }
])
</script>

But get errors:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

enter image description here

At the same time if I do the same in simple html file everything works well:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery-1.10.2.js"></script>
    <script src="jquery.textcomplete.js"></script>

</head>
<body>

<textarea class="textarea" id="textarea" rows="4" cols="50">...</textarea>

<script>

    $('.textarea').textcomplete([
        { // tech companies
            words: ['select', 'from', 'fct_table1', 'fct_table2', 'where', 'column1', 'column2'],
            match: /\b(\w{2,})$/,
            search: function (term, callback) {
                callback($.map(this.words, function (word) {
                    return word.indexOf(term) === 0 ? word : null;
                }));
            },
            index: 1,
            replace: function (word) {
                return word + ' ';
            }
        }
    ]);

</script>
</body>
</html>

How to make the script work inside Django?strong text

>Solution :

<script src="{% static 'js/tasks/jquery-1.10.2.min.js' %} "></script>

Do this for all external scripts.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading