简体   繁体   中英

Variable is not defined (sending from HTML to JS file)

I'm trying to send a variable ( user.id - current user's id ) into my JQuery file which calls a view to delete some data in a database. The problem is that I don't know how to pass user.id into the JS file to be able to append it to url request.

In my template:

<script src="{% static "js/checkTestsSittings.js" %}">var userid = {{ user.id | safe }};</script>

JS file:

$(document).ready(function () {
    alert(userid); // just a test
    request_url = '/check-sittings/'+userid+'/';
    $.ajax({
        url: request_url
    })
});

This is what I see in the console:

在此处输入图片说明

I'm beginner in JS and Django. Could you give me a hint?

You can't add file and code in a single script tag, you should add them independently, like this

<script>var userid = {{ user.id | safe }};</script>
<script src="{% static "js/checkTestsSittings.js" %}"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM