I'm implementing autocomplete with Flask and JavaScript, and I'm trying to put the JavaScript in a different file. My folder structure is like:
- Project
- static
| -js
| autocomplete.js
- templates
| index.html
main.py
If I add the script in index.html
:
<script>
$(function() {
$.ajax({
url: '{{ url_for("autocomplete") }}'
}).done(function (data) {
$('#inputBox').autocomplete({
source: data,
minLength: 1
});
});
});
</script>
It works fine, but when I put the code into /static/js/autocomplete.js
and add <script src="../static/js/autocomplete.js"></script>
in index.html
,
I get a 404:
127.0.0.1 - - [11/Oct/2017 07:54:10] "GET /%7B%7B%20url_for(%22static%22,%22autocomplete%22)%20%7D%7D HTTP/1.1" 404 -
So how I should write the path in url_for()
?