简体   繁体   中英

Serving from Flask I cannot load javascript file

I am trying to build my first Single Page Application following a tutorial but I cannot load the local javascript file.

My Python code:

from flask import Flask, current_app


app = Flask(__name__, static_folder='')

@app.route('/')
def index():
    return current_app.send_static_file('index.html')

if __name__ == "__main__":
    app.run()

My HTML:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title>My Page</title>
    <script
    src="https://code.jquery.com/jquery-3.4.0.min.js"
    integrity="sha256-BJeo0qm959uMBGb65z40ejJYGSgR7REI4+CW1fNKwOg="
    crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.9/angular.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>

    <div ng-controller="IndexController">
        <p>{{message}}</p>
    </div>


    <script src="./static/js/app.js"></script>
</body>
</html>

My JavaScript:

var myApp = angular.module('myApp', []);

myApp.controller('IndexController', function($scope){
    $scope.message = 'binded ok';
});

The folder structure is:

MyApp
 |-static
 | |-js
 |   |-app.js
 |-index.html
 |-server.py

Everything loads okay except for app.js that gives me:

Request URL: http://localhost:5000/static/js/app.js

Status Code: 404 NOT FOUND

EDIT: If I open index.html directly using the browser app.js is loaded. So I think that Flask is creating an issue here.

尝试像这样引用您的JS文件:

<script src="{{ url_for('static', filename='static/js/app.js') }}"></script>

使用app = Flask(__name__, static_folder='', static_url_path='')创建您的App

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